任务我需要使用自制算法进行二进制搜索。
现在该程序应该可以工作,但不幸的是事实并非如此,他比较了它,但问题是他对城市收费。如果未找到,则显示输出中的内容。
本练习的目的是将搜索值与邮政编码进行比较。邮政编码包含邮政编码和城市名称。
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