Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27513

Binary Search

$
0
0
Anyone know how i would convert this to a binary search?

Private Sub btnSearch_Click(sender As System.Object, e As System.EventArgs) Handles btnSearch.Click
Dim first, last, middle As Integer
Dim flag As Boolean = False
first = 0
last = lstStates.Items.Count - 1
Dim letters As String = mtbFirstTwoLetters.Text.ToUpper
While first <= last
middle = CInt((first + last) / 2)
Dim state As String =
CStr(lstStates.Items.Item(middle)).ToUpper
If state.StartsWith(letters) Then
flag = True
Exit While
Else
Dim st As String = state.Substring(0, 2)
If letters > st Then
first = middle + 1
Else
last = middle - 1
End If
End If
End While
If flag Then
txtOutput.Text = lstStates.Items.Item(middle).ToString
Else
txtOutput.Text = "The State Is Not Found"
End If
End Sub
End Class

Viewing all articles
Browse latest Browse all 27513

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>