Searching is one of the most commonly performed tasks in the domain of Computer Science. Many algorithms and data structures exist to make searching more efficient.
In this article, we’ll look at the idea behind Binary Search and how to implement it in JavaScript.
Binary Search is a very simple, intuitive, yet efficient searching algorithm. The only caveat is that it works only sorted arrays, so some preprocessing on our data in order to sort it might be necessary.
Binary Search is a divide-and-conquer algorithm, that divides the array roughly in half every time it checks whether an element of the array is the one we’re looking for.
In other words, we divide the problem into simpler problems until it becomes simple enough to solve them directly. Let’s assume we have a sorted array (in ascending order) and take a look at the steps of binary search:
#javascript #algorithms