DEV Community

Alex Pushkarev
Alex Pushkarev

Posted on

What is binary search?

Some time ago during the job interview for an SDET role, I was asked how I could efficiently find an element in a sorted collection.

I didn't know! No wonder I didn't get the job. So what was the answer?

In fact, there're only two searching algorithms out there:
1) Simple search (just check every element until you find needed)
2) Binary search (that was the one I didn't know)

👉Binary search is a fast way of finding items in a list based on a search term. Suppose you have a list of names and you want to find out if a specific name is present in that list. You can use binary search to find that name as quickly as possible.
👉Binary search can be used to find items on almost any kind of list – a grocery list, a list of movies, a list of stocks, or a list of anything.

The idea is fairly simple and is quickly demonstrated in the picture below:

Image description

In contrast, here’s how the simple sequential search would work:

Image description

By the way - here are 5 reasons learning algorithms will make you a better developer: https://www.youtube.com/watch?v=rGbWHujTeic

Top comments (0)