DEV Community

Alex Pushkarev
Alex Pushkarev

Posted on

2 1

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

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more