Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,844 questions

55,671 answers

573 users

How to implement binary search in VB.NET

1 Answer

0 votes
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
'

 



answered Aug 5, 2019 by avibootz
...