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

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,885 questions

51,811 answers

573 users

How to implement recursive binary search in VB.NET

1 Answer

0 votes
Imports System

Public Class Test
    Public Shared Function recursive_binary_search(arr() As Integer, left As Integer, right As Integer, to_find As Integer) As Integer
        if (right >= left) then
            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
                return recursive_binary_search(arr, left, mid - 1, to_find)
            Else
                return recursive_binary_search(arr, mid + 1, right, to_find)
            End If
        End If
        
        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 = recursive_binary_search(arr, 0, arr.Length - 1, 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

Related questions

1 answer 130 views
1 answer 136 views
136 views asked Aug 5, 2019 by avibootz
1 answer 78 views
1 answer 106 views
1 answer 85 views
1 answer 89 views
1 answer 78 views
...