ADVERTISEMENT
ADVERTISEMENT

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

Here, we are going to demonstrate the BinarySearch() method of Array class in VB.Net.
Submitted by Nidhi, on January 19, 2021

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

Parameters:

  • 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.

Program/Source Code:

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 »

ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.