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:
- Find the middle element of the array.
- Compare the middle element with the target key.
- If equal, return the index.
- If the key is smaller, search the left half of the array.
- If the key is larger, search the right half of the array.
- Repeat the process until the element is found or the search space becomes empty.
These are the following ways to do Binary Search in JavaScript:
Recursive Approach
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.
let recursiveFunction = function (arr, x, start, end) {
// Base Condition
if (start > end) return false;
// Find the middle index
let mid = Math.floor((start + end) / 2);
// Compare mid with given key x
if (arr[mid] === x) return true;
// If element at mid is greater than x,
// search in the left half of mid
if (arr[mid] > x)
return recursiveFunction(arr, x, start, mid - 1);
else
// If element at mid is smaller than x,
// search in the right half of mid
return recursiveFunction(arr, x, mid + 1, end);
}
// Driver code
let arr = [1, 3, 5, 7, 8, 9];
let x = 5;
if (recursiveFunction(arr, x, 0, arr.length - 1)) {
console.log("Element found!");
}
else { console.log("Element not found!"); }
x = 6;
if (recursiveFunction(arr, x, 0, arr.length - 1)) {
console.log("Element found!");
}
else { console.log("Element not found!"); }
Output
Element found! Element not found!
Explanation:
- Binary search is implemented using recursion on a sorted array.
- The function receives the array, starting index (low), ending index (high), and the target element x.
- The middle index is calculated using:
mid = low + (high - low) / 2 - If the middle element is equal to x, its index is returned.
- If the middle element is greater than x, the function recursively searches the left half of the array.
- If the middle element is smaller than x, the function recursively searches the right half of the array.
- If low becomes greater than high, the element is not present, and -1 is returned.
Iterative Approach
In 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.
// Iterative function to implement Binary Search
let iterativeFunction = function (arr, x) {
let start = 0, end = arr.length - 1;
// Iterate while start not meets end
while (start <= end) {
// Find the mid index
let mid = Math.floor((start + end) / 2);
// If element is present at
// mid, return True
if (arr[mid] === x) return true;
// Else look in left or
// right half accordingly
else if (arr[mid] < x)
start = mid + 1;
else
end = mid - 1;
}
return false;
}
// Driver code
let arr = [1, 3, 5, 7, 8, 9];
let x = 5;
if (iterativeFunction(arr, x, 0, arr.length - 1)) {
console.log("Element found!");
}
else {
console.log("Element not found!");
}
x = 8;
if (iterativeFunction(arr, x, 0, arr.length - 1)) {
console.log("Element found!");
}
else {
console.log("Element not found!");
}
Output
Element found! Element found!
Explanation:
- Uses a while loop to implement iterative binary search on a sorted array.
- Initializes low and high pointers to define the search range.
- The middle index is calculated using:
mid = low + (high - low) / 2 - If arr[mid] is less than x, the search continues in the right half.
- If arr[mid] is greater than x, the search continues in the left half.
- If arr[mid] equals x, the index of the element is returned.
- If the search range becomes invalid, the function returns -1, indicating the element is not found.