![]() |
VOOZH | about |
Binary Search is a searching technique that works on the Divide and Conquer approach. It is used to search for any element in a sorted array. Compared with linear, binary search is much faster with a Time Complexity of O(logN), whereas linear search works in O(N) time complexity.
Here is working of Binary Search:
These are the following ways to do Binary Search in JavaScript:
Binary Search is implemented using recursion by repeatedly dividing the sorted array into smaller subarrays until the target element is found or the search range becomes invalid.
Element found! Element not found!
Explanation:
mid = low + (high - low) / 2In this iterative approach, instead of recursion, we use a while loop, and the loop runs until it hits the base condition, i.e. start becomes greater than end.
Element found! Element found!
Explanation:
mid = low + (high - low) / 2