DEV Community

Cover image for Explaining Algorithms to my girlfriend - Binary Search
Daniel Lima
Daniel Lima

Posted on

Explaining Algorithms to my girlfriend - Binary Search

The purpose of this explanation is to help solidify an idea in my mind. To make sure it's easy to understand, I will explain it in a way that my girlfriend, Rafaella, who is very intelligent but not a fan of math, can comprehend. If you also find this explanation helpful, then that's great!

What is a Algorithm ?

Image description

Imagine you're a chicken trying to cross a busy street to get to the other side. You don't want to get hit by a car, so you need to follow a set of steps or rules, just like an algorithm.

First, you need to look both ways to make sure there are no cars coming. Then, you need to strut your stuff across the street quickly, but not too quickly, while avoiding obstacles like pedestrians and stray cats. Finally, you need to celebrate your success on the other side of the street by doing the chicken dance!

Just like how following these steps can help you safely cross the street as a chicken, algorithms help computers solve problems and perform tasks efficiently by following a set of instructions or steps. The steps in an algorithm are like the rules that the chicken needs to follow to safely cross the street, while avoiding obstacles and celebrating their success on the other side.

Binary search ? 🤔

Image description

Let's start explain the "search" part, and after you will understand the "binary".

Imagine you are looking for a Nachos 🌮 recipe in a book with hundreds of recipes. You know the name of the recipe ( nachos ), but you don't know which page it's on. You could flip through every page of the book, but that would take a long time. Instead, you can use a binary search algorithm to find the recipe quickly.

First, you would start by opening the book to the middle and looking at the title of the recipe on that page. If it's not the recipe you're looking for, you can use the alphabetical order of the recipes to determine whether you need to search the pages before or after the middle page. You would then repeat this process, dividing the remaining pages in half and searching the appropriate section, until you find the recipe you're looking for.

Just like how the BINARY search algorithm helps you quickly locate a recipe in a book by dividing the search area in half and checking if the recipe is in the upper or lower half, it can also help computers find data efficiently by dividing a large dataset in half and checking whether the target data is in the upper or lower half. The binary search algorithm is like a skilled chef who knows how to find a recipe quickly by using the alphabetical order of the recipes and dividing the pages into smaller sections!

Let's apply on software contexts

Image description

Imagine you are trying to login at instagram. Every time you do that, the instagram must verify if you really have a account.
For that, the insta needs to search your name ( or @ ) at a database ( the database of our Nachos recipe are the Book of Recipes ) .
In my girlfriend case ( @rafaLindaDemais ) the instagram could start looking for the A letter and up the list until found her account.

But, make more sense if the we start to looking at the half of your database.

By dividing the search area in half with each step, you can quickly narrow down your search and find the recipe you're looking for much more efficiently. In the same way, when Instagram searches for your account, it can use binary search to quickly find your account in their database by dividing the search area in half with each step, rather than searching through every account in the database one by one.

Of course instagram have more ways to do that, but this is a good start for you implementing a better searching optimization at your software and life :)

Image description

Hope this post can be helpful!
For some feedback or more content, follow me on twitter or github

Top comments (0)