自作のアルゴリズムを使った二分探索が必要です。
これでプログラムは機能するはずですが、残念ながらそうではありません。彼はそれを比較しましたが、問題は都市に課金していることです。見つからない場合は出力に表示されます。
この演習の目的は、検索値を郵便番号と比較することです。郵便番号には、郵便番号と都市の名前が含まれています。
Module Module1
Sub Main()
' 0 1 2 3 4 5 6 7 8 9
Dim zipcodes() As String = {"1000", "Brussel", "2000", "Antwerpen", "3000", "Leuven", "8000", "Brugge", "9000", "Gent"}
'Dim zipcodesCount As Integer = 5
'
Dim searchValues() As String = {"0500", "1000", "2000", "3000", "4000", "8000", "9000", "9500"}
'
Dim searchValueIndex As Integer
For searchValueIndex = 0 To 7
Dim searchValue As String = searchValues(searchValueIndex)
'
Dim index As Integer
Dim found As Boolean = False
Dim allesDoorzocht As Boolean = False
Dim uBound, lBound As Integer
uBound = zipcodes.GetUpperBound(0) - 1
lBound = zipcodes.GetLowerBound(0)
Do Until found OrElse allesDoorzocht
index = (lBound + uBound + 1 ) \ 2
If (searchValue = zipcodes(index)) Then
found = True
End If
If uBound <= lBound Then
allesDoorzocht = True
End If
If Not (found OrElse allesDoorzocht) Then
If searchValue > zipcodes(index) Then
lBound = index + 1
Else
uBound = index - 1
End If
End If
Loop
If found Then
Console.WriteLine(searchValue & " is zipcode of " & zipcodes(index + 1))
' Of : Console.WriteLine(searchValue & " is zipcode of " & zipcodes(index + 1))
Else
Console.WriteLine(searchValue & " not found")
End If
Next
'
Console.ReadLine()
End Sub
End Module