Updated April 3, 2023
Introduction of Binary Search JavaScript
Binary Search JavaScript is a searching technique that works on the Divide and Conquers approach. As searching is one of the most commonly performed tasks in the IT field, many data structures and algorithms exist now to make searching much more efficient. Binary search means Half Interval Search; it finds specific elements located in the array i.e. works with only sorted arrays. It is more advantageous than Linear Search Algorithm based on fastness and more efficiency comparatively. Binary Search is a very simple, efficient, and intuitive searching algorithm but one of the limitations is that works only for sorted arrays, hence, this requires some pre-processing before implementing a Binary search of the array. Let us look at How Binary search works, along with its Algorithm and a few examples.
Syntax of Binary Search JavaScript
Basically, any search algorithms would not have Syntax as such but will follow a flow i.e. Flowchart or an Algorithm steps to implement.
Algorithm:
Step 1: First, we need to find the middle element of the array
Step 2: Then, start comparing the middle element with the value which is being searched for i.e. key element
- If the key element is less than that of the middle element, start searching in the left half of the array
- If the key element is more than that of the middle element, start searching in the right half of the array
- If the key element is equal to the middle element, return the index of the middle element
Step 3: Continue the above steps until there is a single element left
Step 4: If the key element is not found, then return -1
To understand the above algorithm in a better way, we shall take an example.
Input array: 2 4 6 8 10 12 14 16 18 20 22
Key element: 16
- Now, let us check for the middle element of the array, i.e. 12
- Will compare 12 with 16: As 16>12, we shall ignore the left part and start searching the right half.
- So, array will be 14 16 18 20 22
- Now, the middle element will be 18.
- Let us compare 18 with 16, As 16<18, we shall ignore the right half and go with the left part.
- So, array will have only two elements i.e 14, 16
- As there are only two elements left in the array, we shall go with the middle element as 14.
- Let us compare now, 16>14, only the right part of the array is considered and the left part is ignored.
- Now, we are left with a single element in the array i.e. 16.
- Compare key element with array element now, 16=16, which means the key element is found As seen in the above example, very few comparisons were done to find the key element.
Example of Binary Search JavaScript
Following are the examples are given below:
Example #1
Binary Search Algorithm in JavaScript in Recursive approach
Code:
<!DOCTYPE html>
<html>
<body>
<h2>Binary Search in JavaScript</h2>
<script>
let binarySearch = function (inputArray, x, startEle, endEle) {
if (startEle>endEle) return false;
let midEle=Math.floor((startEle + endEle)/2);
if (inputArray[midEle]===x) return true;
if(inputArray[midEle] > x)
return binarySearch(inputArray, x, startEle, midEle-1);
else
return binarySearch(inputArray, x, midEle+1, endEle);
}
let inputArray = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22];
let x = 16;
if (binarySearch(inputArray, x, 0, inputArray.length-1))
document.write("Element " + x + " found in input array!<br>");
else document.write("Element " + x + " is not found in the input array!<br>");
x = 9;
if (binarySearch(inputArray, x, 0, inputArray.length-1))
document.write("Element " + x + " found in input array!<br>");
else document.write("Element " + x + " is not found in the input array!<br>");
</script>
</body>
</html>
Output:
Here you can see that the above example which we had solved manually, have given the same input array for the program and the output is as required.
Example #2
Binary Search Algorithm in Iterative manner
Code:
<!DOCTYPE html>
<html>
<body>
<h2>Binary Search in JavaScript</h2>
<script>
constbinaryalgo = (listArray, key) => {
let lowVal = 0
let highVal = listArray.length - 1
while (lowVal<= highVal) {
constmidVal = Math.floor((lowVal + highVal) / 2)
const x = listArray[midVal]
if (x === key) {
return midVal
}
if (x > key) {
highVal = midVal - 1
} else {
lowVal = midVal + 1
}
}
return null
}
document.write("Element is found at index " + binaryalgo([3, 1, 5, 4, 6 ,7, 5, 9, 10], 5) + "<br>");
document.write("Element is found at index " + binaryalgo([3, 1, 5, 4, 6 ,7, 5, 9, 10], 2) + "<br>")
document.write("Element is found at index " + binaryalgo([3, 1, 5, 4, 6 ,7, 5, 9, 10], 10) + "<br>")
</script>
</body>
</html>
Output:
With the input array given, and the key element to be searched, if there is no value available, it will return -1 or null as provided.
Efficiency of Binary Search Algorithm
- Time complexity of Binary search is O(log2n) where n=no. of elements in the array.
- This time complexity is far better than Linear Search, which actually has a time complexity of O(n).
- Binary search works directly on the original array without creating any copies.
- As Binary search needs a sorted array, and hence the time complexity of O(nlogn) is applicable.
- If the array is small in length, then a brute force algorithm can be applied as a better approach.
Conclusion
With this, we shall conclude the topic “Binary Search JavaScript”. We have seen what Binary search means and how is it implemented using an algorithm. We have also manually solved an example and also the same example we did an implementation programmatically. Two type of approaches have been implemented, i.e. Recursive and Iterative approaches. We have also seen the Time complexity taken by Binary search over linear search.
Recommended Articles
This is a guide to Binary Search JavaScript. Here we also discuss the introduction and syntax of binary search javascript along with different examples and its code implementation. You may also have a look at the following articles to learn more –