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

Heroku

Deploy with ease. Manage efficiently. Scale faster.

Leave the infrastructure headaches to us, while you focus on pushing boundaries, realizing your vision, and making a lasting impression on your users.

Get Started

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

đź‘‹ Kindness is contagious

Please consider giving a ❤️ or leaving a kind comment if you found this post helpful!

Thank you!