DEV Community

Claudio Guedes
Claudio Guedes

Posted on • Edited on

Binary Search in Practice

When working with a large input dataset, it may be necessary to adopt different algorithms. Imagine that you need to search in a large list of numbers, for example, 1 million numbers. If you adopt a solution to iterate through the entire list of numbers one index at a time with a conventional loop until you find the target element, in the worst case, this would require 1 million searches (linear time complexity O(n)).

Now, by adopting binary search, you won't need to search more than 19 times in the worst case (logarithmic time complexity O(log(n))), and this number doesn't grow much even if your input data increases significantly.

However, this is only possible with ordered lists.

If you want to know more about this topic, I found a good material on W3Schools: https://www.w3schools.com/dsa/dsa_algo_binarysearch.php.

I also included a simple example of a binary search implementation.

function binarySearch(value, list){
    let left = 0; //start of the list
    let right = list.length - 1; //end of the list

    while ( left <= right ) {
        const middle = Math.floor(( left + right ) / 2); //middle of the list
        const potentialValue = list[middle]; //potential value starts in the middle

        if (value === potentialValue){ //is the potential value what you are looking for?
            return middle; //if so, return the index of the list
        } else if (value < potentialValue){ //is this value smaller than the potential value (middle of the list)?
            right = middle - 1; //if so, it's to the left, and you can ignore everything to the right of the middle
        } else {
            left = middle + 1; //if not, it's to the right, and you can ignore everything to the left of the middle
        }
    }
    //the whole process happens again... until the value you are looking for is found
    return false;
}

Enter fullscreen mode Exit fullscreen mode

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay