Binary Search Algorithm
ファイルから連絡先リスト (2D 配列) を並べ替え、 を使用して名前を検索するプログラムを VB で作成していstarting with
ますuser input
。次に、見つかった名前と、残りの連絡先情報を表示します。問題は、 が1 つの名前Binary Search Algorithm
だけを検索したことです。という名前をすべて見つける必要があります。start with
user input
これまでの私のコードは次のとおりです。
Dim found As Boolean = False
Dim search as String = txtInput.Text
Do
middle = CInt((first + last) / 2) 'calcuate the middle position of the scope
If contacts(middle, 1).ToLower.StartsWith(search) Then 'if the middle name starts with the search String
found = True 'name was found
ElseIf contacts(middle, 1).ToLower < search Then 'if the search name comes after the middle position's value
first = middle + 1 'move the first position to be 1 below the middle
ElseIf contacts(middle, 1).ToLower > search Then 'if the search name comes before the middle position's value
last = middle - 1 'move the last position to be 1 above the middle
End If
Loop Until first > last Or found = True 'loop until the name is not found or the name is found
If found = True Then
For x = 0 To 4 'display the whole list of data for that name
txtDisplay.Text += contacts(middle,x).padLeft(15)
Loop
End If