my binary search isnt working, i think it has to do with the fact that im using a list and its searching thru an array idk could you guys please help me out
List Code:
Code:Option Strict On Public Class Form1 Dim J As Integer = 0 Dim MyArray As New List(Of Integer) Private Sub btnPutNumberIntoArray_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPutNumberIntoArray.Click Dim number As Integer Integer.TryParse(txtNumbers.Text, number) If number < 1 Then MessageBox.Show("Please enter a number greater then zero.") Else MyArray.Add(number) txtNumbers.Clear() End If End Sub Private Sub btnDisplayNumbersInArray_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplayNumbersInArray.Click txtDisplayNumbers.Clear() txtDisplayNumbers.Focus() For J As Integer = 0 To MyArray.Count - 1 txtDisplayNumbers.Text &= MyArray(J).ToString & Environment.NewLine Next End Sub
Binary Search :
Code:Private Sub btnBinarySearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBinarySearch.Click Dim Key As Integer Dim Low As Integer Dim High As Integer Dim Mid As Integer txtBinaryKey.Text = CStr(Key) Low = 0 High = 4 While Low <= High Mid = (High + Low) \ 2 If Key = MyArray(Mid) Then lblBinarySearchLocation.BackColor = Color.Silver lblBinarySearch.Text = "Search Key found" lblBinarySearchLocation.Text = "Array(" & Str(Mid) & ")" Low = High + 1 ElseIf Key < MyArray(Mid) Then High = Mid - 1 ElseIf Key > MyArray(Mid) Then Low = Mid + 1 End If End While If Key <> MyArray(Mid) Then lblBinarySearch.Text = "Search Key not found." lblBinarySearchLocation.BackColor = Color.Red lblBinarySearchLocation.Text = "" End If End Sub




Reply With Quote