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!
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Floor value Kth root of a number using Recursive Binary Search
- Meta Binary Search | One-Sided Binary Search
- Recursive program to linearly search an element in a given array
- Recursive function to do substring search
- Interpolation search vs Binary search
- Why is Binary Search preferred over Ternary Search?
- Linear Search vs Binary Search
- C Program for Anagram Substring Search (Or Search for all permutations)
- Sublist Search (Search a linked list in another list)
- Repeatedly search an element by doubling it after every successful search
- Best First Search (Informed Search)
- C Program for Recursive Insertion Sort
- C Program for Iterative Merge Sort
- C Program for Iterative Quick Sort
- Recursive Programs to find Minimum and Maximum elements of array
- Binary Search functions in C++ STL (binary_search, lower_bound and upper_bound)
- Eggs dropping puzzle (Binomial Coefficient and Binary Search Solution)
- Check if an array is sorted and rotated using Binary Search
- Floor square root without using sqrt() function : Recursive
- Largest number less than or equal to N in BST (Iterative Approach)