Posted by: Sourav | April 9, 2014

Binary Search in VB 6.0


Private Sub Command1_Click()
Dim newarray(5) As Integer
Dim i As Integer

For i = 0 To 4
newarray(i) = i
Next i
For i = 0 To 4
Print newarray(i)
Next i
Dim high, low, level, search As Integer
high = (UBound(newarray))

low = LBound(newarray)
level = (high + low) / 2
search = 4
Dim found As Boolean
found = False

For i = 0 To UBound(newarray) – 1
If search = newarray(i) Then
Print “found ” & search & ” in position ” & i
found = True

End If

If search < level Then
high = level – 1
level = (high + low) / 2
End If

If search > level Then
low = level + 1
level = (high + low) / 2
End If

 

Next i

If found = False Then
Print “not found”
End If

End Sub

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: