1.

Implement Binary Search in Java using recursion.

Answer»

public class Main{ //Recursive method for binary search private static boolean binarySearch(int[] arr, int low, int high, int KEY){ //Calculating Mid. int mid = (low + high)/2; //Base Case. if(low > high) return false; //Checking if the key is found in the middle. if(arr[mid] == key) return true; //Searching on the left half if a key exists there. if(key < arr[mid]) return binarySearch(arr, low, mid-1, key); //Searching on the other half OTHERWISE. return binarySearch(arr, mid+1, high, key); }public static void main(String[] args) { int[] arr = {2, 5, 9, 13, 17, 21, 30}; if(binarySearch(arr, 0, (arr.length-1), 30)) System.out.println(" Element Found. "); else System.out.println(" Element not Found.");}}

In the above CODE, we are finding the middle element each TIME and checking if the element is in the middle or not. If it is not, then we check on which side from the middle it exists. And Recursively searching on the particular subarray. So this way we are reducing the search space by 2 every time. So the search time is very low.



Discussion

No Comment Found