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,872 questions

51,796 answers

573 users

How to implement recursive binary search in Java

1 Answer

0 votes
public class MyClass {
    static int recursive_binary_search(int arr[], int left, int right, int to_find) {        
        if (right >= left) {
            int mid = left + (right - left) / 2;
            if (arr[mid] == to_find)               
                return mid;            
            if (arr[mid] > to_find)               
                return recursive_binary_search(arr, left, mid - 1, to_find);
  
            return recursive_binary_search(arr, mid + 1, right, to_find);        
        }
        return -1;    
    }
    public static void main(String args[]) {
        int arr[] = {1, 2, 3, 4, 21, 13, 30, 50};
         
        int to_find = 13;        
        int result = recursive_binary_search(arr, 0, arr.length - 1, to_find);        
         
        if (result == -1)            
            System.out.println("Not Found");        
        else          
            System.out.println("Found at index: " + result);    
  
    }
}
  
  
/*
run:
  
Found at index: 5
  
*/

 



answered Aug 5, 2019 by avibootz
edited Aug 5, 2019 by avibootz

Related questions

1 answer 77 views
1 answer 106 views
1 answer 84 views
1 answer 88 views
1 answer 78 views
1 answer 82 views
1 answer 97 views
...