×

VB.Net Programs

VB.Net program to demonstrate the BinarySearch() method of Array class

By Nidhi Last Updated : November 11, 2024

Array.BinarySearch() Method in VB.Net

The BinarySearch() method is used to search specified items from the sorted array.

Syntax

Function BinarySearch(ByVal arr() as object, ByVal num as object) as Integer

Parameter(s)

  • Arr: It is the specified sorted array.
  • Num: Specified number to be searched.

Return Value

It returns the index of the searched item. If the item did not find in the specified array then it will return a negative value.

VB.Net code to demonstrate the example of Array.BinarySearch() method

The source code to demonstrate the BinarySearch() method of the Array class is given below. The given program is compiled and executed successfully.

'VB.NET program to demonstrate the BinarySearch() 
'method of Array class.

Imports System

Module Module1
    Sub Main()
        Dim index As Integer
        Dim arr() As Integer = {10, 20, 30, 40, 50}

        index = Array.BinarySearch(arr, 30)

        If (index < 0) Then
            Console.WriteLine("Item not found")
        Else
            Console.WriteLine("Item {0} find at index: {1}", 30, index)
        End If
    End Sub
End Module

Output

Item 30 find at index: 2
Press any key to continue . . .

Explanation

In the above program, we created a module Module1 that contains the Main() function. The Main() function is the entry point for the program.

In the Main() function, we created an array arr with 5 elements. Then we searched item 30 using BinarySearch() method of Array class and return the index. After that, we printed the index of the specified item on the console screen.

VB.Net Data Structure Programs »


Comments and Discussions!

Load comments ↻





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