Pieces of Programming — Binary Search

Kristian Roopnarine
The Startup
Published in
3 min readNov 11, 2019

--

A series where I introduce concepts in programming ranging from data structures to algorithms to programming languages.

Image source (here)

Searching Algorithms

Searching in programming is essential. We always need to find certain values that are stored in different data structures. When I started programming and heard there are different algorithms to search it didn’t make much sense to me, it’s a computer it should find a certain value easier than I could! This was before I understood how stupid computers ACTUALLY are, we have to tell computers how to do EVERYTHING. To understand Binary Search let’s play a little game.

Guess the number

I have a number between 1–100 and you have to guess what it is and I’ll tell you whether your guess was too high or low. How would you approach this? One way would be starting at 1 and working your way up the list. What if my number was 99? You’d have to take 99 guesses before you arrived at the right answer. That would be a method called simple search.

Now I’m pretty sure that’s not how anybody would actually guess, more than likely they’ll start somewhere in the middle, close to 50. Why is this? Well if 50 isn’t the correct guess, you’ve practically eliminated half of the possible answers! Either 50 is too high or too low which narrows…

--

--

Kristian Roopnarine
The Startup

Full Stack Engineer sharing tips and tricks for anyone learning to program. Connect with me on LinkedIn : https://www.linkedin.com/in/kristianroopnarine/