×

VB.Net Programs

VB.Net program to search an item in array using binary search

By Nidhi Last Updated : November 16, 2024

Search an item in array using binary search

Here, we will create an array of integers and then read elements from the user, and search item in the array using binary search. The binary search is used to search item into the sorted array.

Program/Source Code:

The source code to search an item in array using binary search is given below. The given program is compiled and executed successfully.

VB.Net code to search an item in array using binary search

'VB.Net program to search an item in array using binary search.

Module Module1

    Sub Main()
        Dim arr As Integer() = New Integer(5) {}
        Dim item As Integer = 0
        Dim flag As Integer = 0

        Dim first As Integer = 0
        Dim last As Integer = 0
        Dim middle As Integer = 0
        Dim i As Integer = 0

        Console.WriteLine("Enter array elements: ")
        For i = 0 To 4 Step 1
            Console.Write("Element[{0}]: ", i)
            arr(i) = Integer.Parse(Console.ReadLine())
        Next

        Console.WriteLine("Enter item for searching: ")
        item = Integer.Parse(Console.ReadLine())

        first = 0
        last = 4
        middle = (first + last) / 2

        While first <= last
            If (arr(middle) < item) Then
                first = middle + 1
            ElseIf (arr(middle) = item) Then
                flag = middle
                GoTo out
            Else
                last = middle - 1
            End If
            middle = (first + last) / 2
        End While
out:
        If flag <> 0 Then
            Console.WriteLine("Item found at index {0} in array", flag)
        Else
            Console.WriteLine("Item is not found")
        End If
    End Sub
    
End Module

Output

Enter array elements:
Element[0]: 10
Element[1]: 20
Element[2]: 30
Element[3]: 45
Element[4]: 66
Enter item for searching:
45
Item found at index 3 in array
Press any key to continue . . .

Explanation

In the above program, we created a module Module1 that contains a function Main(). In the Main() we created an array arr of five elements and five variables item, flag, first , last, and middle that are initialized with 0.

Console.WriteLine("Enter array elements: ")
For i = 0 To 4 Step 1
    Console.Write("Element[{0}]: ", i)
    arr(i) = Integer.Parse(Console.ReadLine())
Next

In the above code, we read the elements of the array from the user.

Console.WriteLine("Enter item for searching: ")
item = Integer.Parse(Console.ReadLine())

first = 0
last = 4
middle = (first + last) / 2

While first <= last
    If (arr(middle) < item) Then
        first = middle + 1
    ElseIf (arr(middle) = item) Then
        flag = middle
        GoTo out
    Else
        last = middle - 1
    End If
    middle = (first + last) / 2
End While
out:
If flag <> 0 Then
    Console.WriteLine("Item found at index {0} in array", flag)
Else
    Console.WriteLine("Item is not found")
End If

In the above code, we used binary search technique; in the binary search we searched the item from sorted array. Here, we used divide and conquer methodology by finding mid and then divide list for searching given item. When we found the item then print the location of item on the console screen otherwise it will print "Item is not found" message on the console screen.

VB.Net Array Programs »


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.