Hello and welcome to
‘Algorithms for everyone!’
In this episode of algorithms for everyone,
we are going to be looking at Binary Search.
Why are we doing it? So that the next time
you are at lunch having your lobster burger
hotdog soufflé, and Jimmy starts scoffing
at your choice of bow tie for the occasion,
you can turn around and be like,
Aha! But Binary Search! And leave on a cloud
of smug.
NO but really,
Binary Search is pretty important in computer
science. Imagine you are searching for your
favourite restaurant, ‘Pattys Pop Tarts,’
The finest toasted Pop tarts around
In this huge list of alphabetically sorted
restaurants .
Now realistically, you are not going to go
through each line one by one. That would be
silly! You already know that the list is alphabetically
sorted, and you also know that P is the
..16th letter of the alphabet.
Meaning that we don’t need to search the
beginning of the list, Let’s go directly
to the middle of our list, and make a decision
whether to search back, or forward, from there.
Well you will be pleased to know that a computer
does exactly the same in order to save time,
just a bit more mathsy.
In order to solve its search problem, it will
use the binary search algorithm, to literally
half the number of restaurants to search through,
again and again, until we find the position
of our restaurant on the list.
Lets have a look at this in an example.
Now, there are two things that must be true
for this to work. One - Our list must be in
order, so sorted.
Two - the element we are looking for must
exist in the list, otherwise, our search will
return a null.
So once we have that in mind, pick a card
any card. Don’t tell me, just keep that
number in your head, from 1, or an ace, to
10.
Now if I didn’t care how long it would take
me to guess your card, I would simply ask
you, is your card a one, is your card a two,
is your card a three, and so on until I found
your card. It would take me exactly 6 goes
to found your card. This actually has a name,
and is called, the Simple Search.
But if I wanted to find out smarter and quicker,
I would ask you to tell me if I guessed your
card, or if its higher or lower.
So would start by picking the middle card,
so I can elicited HALF the deck form the get
go.
Is your card a 5,
Higher!
Cool, now we roughly half that again,
Is your card a 8
Lower
Is your card a 7
Lower
Your card is a 6
Whatever the number is, I can guess it in
a maximum of 4 guesses, if I use this halving
algorithm known as binary search. Woaaah!
Magic! No not really, ALGORITHMS!
What is also cool, that I saved time! Meaning
that I guessed the card in 4 goes as opposed
to the 6, or at its worst case 10 goes, if
the number was 10.
Now obviously, in the previous example, asking
ten times is not the end of the world, but
what if instead of searching for a number
from 1 to 10, I wanted to search for a number
from 1 to 4 billion! AND imagine that each
time We go through the motion of me asking
you your number, and you giving me a response,
that takes us 3 seconds.
is your number 6 hundred and forty five thousand
two hundred and twenty nine..
no just kill me no
At its worst case, so if the randomly chosen
number was literally 4 billion, this would
take you 380.51750380518 years to find out
in simple search. And obviously, 4 billion
questions to ask.
Now this is where people really appreciate
binary search!
In a great book called Grokking Algorithms,
author Aditya Bhargava shows us with the power
of numbers what a difference this algorithm
really can make.
So when we have a list of 4 billion numbers,
it can take up to 4 billion guesses. This
makes the maximum number of guesses the same
size as the list. This is called linear time.
In binary search however, this is different,
just as with our smaller list of ten cards
having a maximum of 4 guesses, if we had 4
million cards, at most it would take us 32
guesses to find the card.
MENTAL!
We have just gone from a maximum of 4 billion
guesses, to JUST 32 GUESSES!!!
We have gone from over 380 years being the
worst case scenario to finding our number,
to just 96 seconds!!!!
I mean if that isn’t cool than I don’t
know what is.
We are now also learning that binary search
runs in logarithmic time.
Or, LOG TIME. I will leave you to google that
on your own, or perhaps I will do a another
video on this separately as its a whole new
rabbit hole to go into.
This linear time for simple searches, and
logarithmic time for binary searches are what
is known as run times for algorithms, and
they are also part of what is known as Big
O Notation. Another chapter for later
So lets recap:
Binary search is a halving algorithm that
is faster than simple search
Binary search is a lot faster than simple
search when the lists get bigger and bigger
Binary search is what is known as O (log n)
Simple search is what is known as O(n)
Algorithm times are written in big O notation
And where do we go from here?
Well a good place to start if you want to
add some more depth tho your knowledge is
understanding Logarithms and Big O Notation!
I will let you take
it from here,
