We basically ignore half of the elements just after one comparison.
- Compare x with the middle element.
- If x matches with middle element, we return the mid index.
- Else If x is greater than the mid element, then x can only lie in right half subarray after the mid element. So we recur for right half.
- Else (x is smaller) recur for the left half.
Please refer complete article on Binary Search for more details!
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Recursive program to linearly search an element in a given array
- Recursive function to do substring search
- Meta Binary Search | One-Sided Binary Search
- Why is Binary Search preferred over Ternary Search?
- C Program for Anagram Substring Search (Or Search for all permutations)
- Linear Search vs Binary Search
- Interpolation search vs Binary search
- C Program for Recursive Insertion Sort
- C Program for Iterative Quick Sort
- C Program for Iterative Merge Sort
- Binary Search
- Binary Search in PHP
- The Ubiquitous Binary Search | Set 1
- Binary Search in Java
- Binary Search a String