0% found this document useful (0 votes)
39 views1 page

Binary Search

The document contains a Visual Basic module that implements a binary search algorithm to find an integer in a predefined array. It includes a method to search for a value and another method to print the array elements. The search method outputs the index of the found integer or indicates if the integer is not present.

Uploaded by

Muhammad Haris
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views1 page

Binary Search

The document contains a Visual Basic module that implements a binary search algorithm to find an integer in a predefined array. It includes a method to search for a value and another method to print the array elements. The search method outputs the index of the found integer or indicates if the integer is not present.

Uploaded by

Muhammad Haris
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Imports System.

Console

Module BinarySearch
Dim Array() As Integer = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
Dim First, Middle, Last As Integer
Dim Found, Failed As Boolean

Sub Search(ByVal vaLue As Integer)


Found = False
Failed = False
First = 0
Last = Array.Length - 1
While Found = False And Failed = False
Middle = (First + Last) / 2
If Array(Middle) = vaLue Then
Found = True
Else
If First >= Last Then
Failed = True
ElseIf Array(Middle) < vaLue Then
First = Middle + 1
Else
Last = Middle - 1
End If
End If
End While
If Found = True Then
WriteLine("The search integer is at " & Middle & ".")
Else
WriteLine("The search integer is not present.")
End If
End Sub

Sub PrintArray()
For i = 0 To Array.Length - 1
WriteLine(Array(i))
Next
End Sub
End Module

You might also like