Java Program for Binary Search Using Recursion
binarySearch(arr, low, high, key) that checks the middle element and recursively searches the left or right half until the key is found or the range is empty.Examples
How to Think About It
Algorithm
Code
public class BinarySearchRecursive { public static int binarySearch(int[] arr, int low, int high, int key) { if (low > high) { return -1; } int mid = low + (high - low) / 2; if (arr[mid] == key) { return mid; } else if (key < arr[mid]) { return binarySearch(arr, low, mid - 1, key); } else { return binarySearch(arr, mid + 1, high, key); } } public static void main(String[] args) { int[] arr = {1, 3, 5, 7, 9}; int key = 7; int result = binarySearch(arr, 0, arr.length - 1, key); if (result == -1) { System.out.println("Key not found"); } else { System.out.println("Key found at index " + result); } } }
Dry Run
Let's trace searching for key 7 in array {1, 3, 5, 7, 9} using recursion.
Initial call
low=0, high=4, mid=2, arr[mid]=5, key=7; key > arr[mid], search right half
Second call
low=3, high=4, mid=3, arr[mid]=7, key=7; key == arr[mid], return 3
| Call | low | high | mid | arr[mid] | key | Action |
|---|---|---|---|---|---|---|
| 1 | 0 | 4 | 2 | 5 | 7 | key > arr[mid], search right half |
| 2 | 3 | 4 | 3 | 7 | 7 | key == arr[mid], return index 3 |
Why This Works
Step 1: Divide the array
The code finds the middle index using mid = low + (high - low) / 2 to avoid overflow and splits the array into halves.
Step 2: Compare middle element
It compares the middle element with the key using arr[mid] == key to check if the search is successful.
Step 3: Recursive search
If the key is smaller or larger, it calls itself recursively on the left or right half using binarySearch(arr, low, mid - 1, key) or binarySearch(arr, mid + 1, high, key).
Step 4: Stop condition
When low > high, it means the key is not in the array, so it returns -1.
Alternative Approaches
public class BinarySearchIterative { public static int binarySearch(int[] arr, int key) { int low = 0, high = arr.length - 1; while (low <= high) { int mid = low + (high - low) / 2; if (arr[mid] == key) return mid; else if (key < arr[mid]) high = mid - 1; else low = mid + 1; } return -1; } public static void main(String[] args) { int[] arr = {1, 3, 5, 7, 9}; int key = 7; int result = binarySearch(arr, key); if (result == -1) System.out.println("Key not found"); else System.out.println("Key found at index " + result); } }
import java.util.Arrays; public class BinarySearchBuiltIn { public static void main(String[] args) { int[] arr = {1, 3, 5, 7, 9}; int key = 7; int result = Arrays.binarySearch(arr, key); if (result < 0) System.out.println("Key not found"); else System.out.println("Key found at index " + result); } }
Complexity: O(log n) time, O(log n) space
Time Complexity
Binary search divides the array in half each time, so it runs in logarithmic time O(log n).
Space Complexity
Recursive calls add to the call stack, so space complexity is O(log n) due to recursion depth.
Which Approach is Fastest?
Iterative binary search uses O(1) space and is faster in practice due to no recursion overhead.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Recursive binary search | O(log n) | O(log n) | Learning recursion and divide-and-conquer |
| Iterative binary search | O(log n) | O(1) | Memory-efficient and practical use |
| Java built-in Arrays.binarySearch | O(log n) | O(1) | Quick and optimized search without manual code |
low > high to stop recursion and avoid infinite calls.