Imports System
Public Class Test
Public Shared Function binary_search(arr() As Integer, to_find As Integer) As Integer
Dim left As Integer = 0
Dim right As Integer = arr.Length - 1
Do While left <= right
Dim mid As Integer = left + Convert.ToInt32((right - left) / 2)
If (arr(mid) = to_find) Then
return mid
End If
If (arr(mid) < to_find) Then
left = mid + 1
else
right = mid - 1
End If
Loop
return -1
End Function
Public Shared Sub Main()
Dim arr() As Integer = { 2, 3, 6, 7, 12, 13, 17, 19, 21 }
Dim to_find As Integer = 13
Dim result As Integer = binary_search(arr, to_find)
If (result = -1) Then
Console.WriteLine("Not found")
else
Console.WriteLine("Found at index: {0}", result)
End If
End Sub
End Class
' run:
'
' Found at index: 5
'