# Recursive Binary Search in Java

The binary search algorithm is one of the commonly used algorithms in programming. It is used to search and find an element in a sorted array.

The binary search algorithm is a highly efficient search technique used to locate a specific element in a sorted dataset. It works by repeatedly dividing the dataset in half and comparing the target value with the middle value until the target value is discovered or determined to be absent.

Binary search is based on the divide-and-conquer principle, which involves breaking down a large problem into smaller, more manageable subproblems. In the case of binary search, the large problem is finding a specific element in a sorted dataset. The subproblems are then recursively solved until the target element is found or determined to be absent.

The divide and conquer principle are a powerful technique that can be used to solve a wide variety of problems.

The divide and conquer principle in programming is a problem-solving technique that involves breaking down a large problem into smaller, more manageable subproblems. The subproblems are then solved recursively until the original problem is solved.

• Sorting algorithms, such as quicksort and merge sort
• Searching algorithms, such as binary search
• String processing algorithms, such as the Knuth-Morris-Pratt algorithm
• Graph algorithms, such as Dijkstra's algorithm and Prim's algorithm
• Numerical algorithms, such as the Fast Fourier Transform (FFT)

## How does Binary Search algorithm work?

The binary search algorithm is a divide and conquer algorithm that searches for a specific element in a sorted array.

Note that the collection of elements/array must be sorted for the algorithm to work efficiently.

Initialize the search interval. The search interval is the range of elements in the dataset that are currently being considered. Initially, the search interval is the entire dataset.

Step 1: Find the middle element of the search interval. It is done by calculating the average of the first and last elements in the search interval.

Step 2: Compare the target value to the middle element. If the target value is equal to the middle element, then the search is successful and the index of the target element is returned.

Step 3: If the target value is less than the middle element, then the search interval is narrowed down to the left half of the original search interval.

Step 4: If the target value is greater than the middle element, then the search interval is narrowed down to the right half of the original search interval.

Step 5: Steps 2 through 5 are repeated until the target element is found or the search interval becomes empty.

## Binary Search Algorithm

#### Note: X is the element that we want to search.

• Compare x with the middle element.
• If x matches with the middle element, we return the mid index.
• Else If x is greater than the mid element, then x can only lie in the right half subarray after the mid element. So, we recur for the right half.
• Else (x is smaller) recur for the left half.

BinarySearch.java

Output:

```Element found at index 3
```

### Complexity

Best Case: For the best case the time complexity is O(1). The best case occurs when the target element is the middle element of the array. In this case, the algorithm only needs to compare the target element to the middle element and returns the index of the target element.

Average Case: For the average case the time complexity is O(n). The average case occurs when the target element is not the middle element of the array. In this case, the algorithm will need to divide the array in half and compare the target element to the middle element. The algorithm will then recursively search the appropriate half of the array. The process will continue until the target element is found or the array becomes empty.

Worst Case: The worst case complexity is O(n log n). The worst case occurs when the target element is not in the array. In this case, the algorithm will divide the array in half and compare the target element to the middle element. The algorithm will then recursively search the appropriate half of the array. This process will continue until the array becomes empty.

## Applications of Binary Search:

• Find an element in a sorted array.
• To find if n is a square of an integer.
• Find the first value greater than or equal to x in a given array of sorted integers.
• Find the frequency of a given target value in an array of integers.
• Find the peak of an array which increases and then decreases.
• A sorted array is rotated n times. Search for a target value in the array.
• To implement a Dictionary.
• Debugging a linear piece of code.
• Figuring out resource requirements for a large system.
• Find values in sorted collection.
• Semiconductor test programs.
• Numerical solutions to an equation.

• Binary search offers a number of advantages over other search algorithms, such as linear search:
• Speed: Binary search is much faster than linear search, especially for large datasets.
• Efficiency: Binary search is more efficient than linear search, as it requires fewer comparisons to find the target element.
• Reliability: Binary search is a reliable algorithm, as it always returns the correct index of the target element, or -1 if the target element is not found.

One disadvantage of binary search is that it requires the dataset to be sorted. This can add overhead to the search algorithm, especially for large datasets. Additionally, binary search is not as effective as linear search for searching small datasets.