Binary Search is a searching technique used to search an element in a sorted array. In this article, we will learn about how to implement Binary Search in PHP using iterative and recursive way. Given a array of numbers, we need to search for the presence of element x in the array using Binary Search.
Input : 1 2 3 4 5 5 Output : 5 Exists Input : 1 5 8 0 Output : 0 Doesnt Exist
This searching technique is more efficient than linear search.
1) Sort the array as binary search only works on sorted ranges
2) Compute the middle element if the element we wish to search is greater than the middle element search on the right side else search on the left.
3) Return True if the element is found.
Recursion is a way where we repeatedly call the same function until a base condition is matched to end the recursion.
Proceeding with the steps in method 1 here we use the same idea by just changing the parameters of the function in recursive manner and breaking down the problem.
- Meta Binary Search | One-Sided Binary Search
- Linear Search vs Binary Search
- Interpolation search vs Binary search
- Binary Search
- Uniform Binary Search
- Binary Search in Java
- Binary Search using pthread
- Binary Search a String
- Variants of Binary Search
- The Ubiquitous Binary Search | Set 1
- Randomized Binary Search Algorithm
- 8085 program for Binary search
- A Problem in Many Binary Search Implementations
- Binary Search (bisect) in Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.