﻿'Visual Basic 2012 Binair Zoeken in Arrays Arrays Module BinarySearch Sub Main() Dim base As Integer = 2 Dim count As Integer = 10 Dim upperbound As Integer = count - 1 Dim numbers(upperbound) As Integer ' Dim index As Integer For index = 0 To upperbound numbers(index) = (index + 1) * base Next ' Dim number As Integer For number = base - 1 To base * count + 1 ' binary search upperbound = count - 1 Dim lowerbound As Integer = 0 Dim found As Boolean = False Dim exhausted As Boolean = False Do Until found OrElse exhausted index = (lowerbound + upperbound) \ 2 ' (1) found = (number = numbers(index)) ' (2) exhausted = (upperbound <= lowerbound) ' (3) If Not (found OrElse exhausted) Then If number > numbers(index) Then ' (4) lowerbound = index + 1 Else upperbound = index - 1 End If End If Loop ' output If found Then Console.Write(number & " found at index " & index) Else Console.Write(number & " not found") End If If exhausted Then Console.Write(", search exhausted") Else Console.Write(", search not exhausted") End If Console.WriteLine(", last searched " & lowerbound & " to " & upperbound) Next ' Console.ReadLine() End Sub End Module 'Bezoek www.vbvoorbeelden.be voor meer Visual Basic voorbeelden. 'Copyright - De Wolf / vbvoorbeelden - 2003-2012 - Alle rechten voorbehouden.