Binary Search is an algorithm that can find the index of an element in a sorted array data structure. You've likely used Binary Search it in everyday life without even realizing it. 

#compsci #programming #100SecondsOfCode

🔗 Resources

Binary Search https://en.wikipedia.org/wiki/Binary_search_algorithm
Big-O in 100 Seconds https://youtu.be/g2o22C3CRfU
Recursion in 100 seconds https://youtu.be/rf60MejMz3E

🔥 Get More Content - Upgrade to PRO

Upgrade to Fireship PRO at https://fireship.io/pro
Use code lORhwXd2 for 25% off your first payment. 

🎨 My Editor Settings

- Atom One Dark 
- vscode-icons
- Fira Code Font

🔖 Topics Covered

- Binary Search in JavaScript
- How does binary search work?
- When was binary search first used?
- How code recursive binary search?
- Big-O Time complexity of binary search
2024-02-04T05:30:53.421Z
nova-2
2-general-nova
2024-01-09.29447
30089e05-99d1-4376-b32e-c263170674af
788716bd-8f82-4ab0-8398-a95a883473eb
c5bd14874e3bb26155bedcec2cc7b07ae2bc402cb03f161c1943b2a0a9ab55a3
67875a7f-c9c4-48a0-aa55-5bdb8a91c34a
deprecated
Binary search.
An algorithm for finding an element in a sorted array by continuously chopping the search area in half.
The first known implementation took place around 200 BC in ancient Babylon on clay tablets used for record keeping, but your brain has likely implemented binary search in everyday life without you even knowing it.
Imagine looking for word like magic in the dictionary.
You wouldn't flip every page until you get to m.
Instead, you open it in the middle.
If you overshoot to the q's, bus, then you go to the middle of that chunk.
This time, you undershoot to the j's, so you do the same thing again, but in the other direction until finally you find the page you're looking more.
Easy enough.
Now it's time for your technical interview.
You might be given a question, like, given a sorted array, write a function that returns for the index for the given element.
The easy way to solve that problem is with a simple for loop.
Just loop over every element in the array until you find the one you're looking before.
That works, but you fail the interview because you need to go faster.
A regular loop results in linear time complexity, but you can do better than that.
Here's how we might map out a binary search on a whiteboard.
But first, we need to figure out the middle index.
Instead of starting at index 0, we start in the middle.
If it's equal to the target, then we return that index.
But if that element is greater than the target, then we know the target must be somewhere on the left.
So we find the middle of that slice of the array.
But if it's less than the target, then we know the target is somewhere on right, so we repeat the process for that slice of the array.
The result is a much faster algorithm with logarithmic time complexity because it's able to divide and conquer.
Now to implement the code, we could use an iterative approach with a while loop or a recursive function.
Either way is acceptable, but let's define a recursive function in JavaScript.
The function takes the target value as an argument as well as a starting and ending index.
First, we have a base condition to stop looping when it reaches the end of the the array, at which point we know the target is not in the array.
From there, we compute the middle index, then we check to see if that middle index is equal to the target, at which point we can return because we found the element we're looking for.
Otherwise, we need to continue searching and that's where recursion comes into play.
If the middle value is greater than the target, then we'll call the same function, but this time we'll put the ending index at the middle.
The other possibility is that the middle value is less than the target, in which case we can also to recurse, but this time, we'll put the starting index in the middle.
And now we have an algorithm that can find an element in a sorted array much faster than a regular loop.
If you wanna see more videos about algorithms on this channel.
Hit the like button and let me know in the comments.
Thanks for watching and I will see you in the next one.

Speaker 0: Binary search. An algorithm for finding an element in a sorted array by continuously chopping the search area in half. The first known implementation took place around 200 BC in ancient Babylon on clay tablets used for record keeping, but your brain has likely implemented binary search in everyday life without you even knowing it. Imagine looking for word like magic in the dictionary. You wouldn't flip every page until you get to m.

Instead, you open it in the middle. If you overshoot to the q's, bus, then you go to the middle of that chunk. This time, you undershoot to the j's, so you do the same thing again, but in the other direction until finally you find the page you're looking more. Easy enough. Now it's time for your technical interview.

You might be given a question, like, given a sorted array, write a function that returns for the index for the given element. The easy way to solve that problem is with a simple for loop. Just loop over every element in the array until you find the one you're looking before. That works, but you fail the interview because you need to go faster. A regular loop results in linear time complexity, but you can do better than that.

Here's how we might map out a binary search on a whiteboard. But first, we need to figure out the middle index. Instead of starting at index 0, we start in the middle. If it's equal to the target, then we return that index. But if that element is greater than the target, then we know the target must be somewhere on the left.

So we find the middle of that slice of the array. But if it's less than the target, then we know the target is somewhere on right, so we repeat the process for that slice of the array. The result is a much faster algorithm with logarithmic time complexity because it's able to divide and conquer. Now to implement the code, we could use an iterative approach with a while loop or a recursive function. Either way is acceptable, but let's define a recursive function in JavaScript.

The function takes the target value as an argument as well as a starting and ending index. First, we have a base condition to stop looping when it reaches the end of the the array, at which point we know the target is not in the array. From there, we compute the middle index, then we check to see if that middle index is equal to the target, at which point we can return because we found the element we're looking for. Otherwise, we need to continue searching and that's where recursion comes into play. If the middle value is greater than the target, then we'll call the same function, but this time we'll put the ending index at the middle.

The other possibility is that the middle value is less than the target, in which case we can also to recurse, but this time, we'll put the starting index in the middle. And now we have an algorithm that can find an element in a sorted array much faster than a regular loop. If you wanna see more videos about algorithms on this channel. Hit the like button and let me know in the comments. Thanks for watching and I will see you in the next one.
Binary search. An algorithm for finding an element in a sorted array by continuously chopping the search area in half. The first known implementation took place around 200 BC in ancient Babylon on clay tablets used for record keeping, but your brain has likely implemented binary search in everyday life without you even knowing it. Imagine looking for word like magic in the dictionary. You wouldn't flip every page until you get to m. Instead, you open it in the middle. If you overshoot to the q's, bus, then you go to the middle of that chunk. This time, you undershoot to the j's, so you do the same thing again, but in the other direction until finally you find the page you're looking more.
Easy enough. Now it's time for your technical interview. You might be given a question, like, given a sorted array, write a function that returns for the index for the given element. The easy way to solve that problem is with a simple for loop. Just loop over every element in the array until you find the one you're looking before.
programming
That works, but you fail the interview because you need to go faster. A regular loop results in linear time complexity, but you can do better than that. Here's how we might map out a binary search on a whiteboard. But first, we need to figure out the middle index.
machine learning
Instead of starting at index 0, we start in the middle. If it's equal to the target, then we return that index. But if that element is greater than the target, then we know the target must be somewhere on the left. So we find the middle of that slice of the array. But if it's less than the target, then we know the target is somewhere on right, so we repeat the process for that slice of the array.
The result is a much faster algorithm with logarithmic time complexity because it's able to divide and conquer. Now to implement the code, we could use an iterative approach with a while loop or a recursive function. Either way is acceptable, but let's define a recursive function in JavaScript. The function takes the target value as an argument as well as a starting and ending index.
programming
First, we have a base condition to stop looping when it reaches the end of the the array, at which point we know the target is not in the array. From there, we compute the middle index, then we check to see if that middle index is equal to the target, at which point we can return because we found the element we're looking for. Otherwise, we need to continue searching and that's where recursion comes into play. If the middle value is greater than the target, then we'll call the same function, but this time we'll put the ending index at the middle. The other possibility is that the middle value is less than the target, in which case we can also to recurse, but this time, we'll put the starting index in the middle.
And now we have an algorithm that can find an element in a sorted array much faster than a regular loop. If you wanna see more videos about algorithms on this channel. Hit the like button and let me know in the comments. Thanks for watching and I will see you in the next one.
streaming
Binary search. An algorithm for finding an element in a sorted array by continuously chopping the search area in half. The first known implementation took place around 200 BC in ancient Babylon on clay tablets used for record keeping, but your brain has likely implemented binary search in everyday life without you even knowing it. Imagine looking for word like magic in the dictionary. You wouldn't flip every page until you get to m. Instead, you open it in the middle. If you overshoot to the q's, bus, then you go to the middle of that chunk. This time, you undershoot to the j's, so you do the same thing again, but in the other direction until finally you find the page you're looking more. Easy enough. Now it's time for your technical interview. You might be given a question, like, given a sorted array, write a function that returns for the index for the given element. The easy way to solve that problem is with a simple for loop. Just loop over every element in the array until you find the one you're looking before. That works, but you fail the interview because you need to go faster. A regular loop results in linear time complexity, but you can do better than that. Here's how we might map out a binary search on a whiteboard. But first, we need to figure out the middle index. Instead of starting at index 0, we start in the middle. If it's equal to the target, then we return that index. But if that element is greater than the target, then we know the target must be somewhere on the left. So we find the middle of that slice of the array. But if it's less than the target, then we know the target is somewhere on right, so we repeat the process for that slice of the array. The result is a much faster algorithm with logarithmic time complexity because it's able to divide and conquer. Now to implement the code, we could use an iterative approach with a while loop or a recursive function. Either way is acceptable, but let's define a recursive function in JavaScript. The function takes the target value as an argument as well as a starting and ending index. First, we have a base condition to stop looping when it reaches the end of the the array, at which point we know the target is not in the array. From there, we compute the middle index, then we check to see if that middle index is equal to the target, at which point we can return because we found the element we're looking for. Otherwise, we need to continue searching and that's where recursion comes into play. If the middle value is greater than the target, then we'll call the same function, but this time we'll put the ending index at the middle. The other possibility is that the middle value is less than the target, in which case we can also to recurse, but this time, we'll put the starting index in the middle. And now we have an algorithm that can find an element in a sorted array much faster than a regular loop. If you wanna see more videos about algorithms on this channel. Hit the like button and let me know in the comments. Thanks for watching and I will see you in the next one.
Binary
binary
search.
search
An
an
algorithm
algorithm
for
for
finding
finding
an
an
element
element
in
in
a
a
sorted
sorted
array
array
by
by
continuously
continuously
chopping
chopping
the
the
search
search
area
area
in
in
half.
half
The
the
first
first
known
known
implementation
implementation
took
took
place
place
around
around
200
200
BC
bc
in
in
ancient
ancient
Babylon
babylon
on
on
clay
clay
tablets
tablets
used
used
for
for
record
record
keeping,
keeping
but
but
your
your
brain
brain
has
has
likely
likely
implemented
implemented
binary
binary
search
search
in
in
everyday
everyday
life
life
without
without
you
you
even
even
knowing
knowing
it.
it
Imagine
imagine
looking
looking
for
for
word
word
like
like
magic
magic
in
in
the
the
dictionary.
dictionary
You
you
wouldn't
wouldn't
flip
flip
every
every
page
page
until
until
you
you
get
get
to
to
m.
m
Instead,
instead
you
you
open
open
it
it
in
in
the
the
middle.
middle
If
if
you
you
overshoot
overshoot
to
to
the
the
q's,
q's
bus,
bus
then
then
you
you
go
go
to
to
the
the
middle
middle
of
of
that
that
chunk.
chunk
This
this
time,
time
you
you
undershoot
undershoot
to
to
the
the
j's,
j's
so
so
you
you
do
do
the
the
same
same
thing
thing
again,
again
but
but
in
in
the
the
other
other
direction
direction
until
until
finally
finally
you
you
find
find
the
the
page
page
you're
you're
looking
looking
more.
more
Easy
easy
enough.
enough
Now
now
it's
it's
time
time
for
for
your
your
technical
technical
interview.
interview
You
you
might
might
be
be
given
given
a
a
question,
question
like,
like
given
given
a
a
sorted
sorted
array,
array
write
write
a
a
function
function
that
that
returns
returns
for
for
the
the
index
index
for
for
the
the
given
given
element.
element
The
the
easy
easy
way
way
to
to
solve
solve
that
that
problem
problem
is
is
with
with
a
a
simple
simple
for
for
loop.
loop
Just
just
loop
loop
over
over
every
every
element
element
in
in
the
the
array
array
until
until
you
you
find
find
the
the
one
one
you're
you're
looking
looking
before.
before
That
that
works,
works
but
but
you
you
fail
fail
the
the
interview
interview
because
because
you
you
need
need
to
to
go
go
faster.
faster
A
a
regular
regular
loop
loop
results
results
in
in
linear
linear
time
time
complexity,
complexity
but
but
you
you
can
can
do
do
better
better
than
than
that.
that
Here's
here's
how
how
we
we
might
might
map
map
out
out
a
a
binary
binary
search
search
on
on
a
a
whiteboard.
whiteboard
But
but
first,
first
we
we
need
need
to
to
figure
figure
out
out
the
the
middle
middle
index.
index
Instead
instead
of
of
starting
starting
at
at
index
index
0,
0
we
we
start
start
in
in
the
the
middle.
middle
If
if
it's
it's
equal
equal
to
to
the
the
target,
target
then
then
we
we
return
return
that
that
index.
index
But
but
if
if
that
that
element
element
is
is
greater
greater
than
than
the
the
target,
target
then
then
we
we
know
know
the
the
target
target
must
must
be
be
somewhere
somewhere
on
on
the
the
left.
left
So
so
we
we
find
find
the
the
middle
middle
of
of
that
that
slice
slice
of
of
the
the
array.
array
But
but
if
if
it's
it's
less
less
than
than
the
the
target,
target
then
then
we
we
know
know
the
the
target
target
is
is
somewhere
somewhere
on
on
right,
right
so
so
we
we
repeat
repeat
the
the
process
process
for
for
that
that
slice
slice
of
of
the
the
array.
array
The
the
result
result
is
is
a
a
much
much
faster
faster
algorithm
algorithm
with
with
logarithmic
logarithmic
time
time
complexity
complexity
because
because
it's
it's
able
able
to
to
divide
divide
and
and
conquer.
conquer
Now
now
to
to
implement
implement
the
the
code,
code
we
we
could
could
use
use
an
an
iterative
iterative
approach
approach
with
with
a
a
while
while
loop
loop
or
or
a
a
recursive
recursive
function.
function
Either
either
way
way
is
is
acceptable,
acceptable
but
but
let's
let's
define
define
a
a
recursive
recursive
function
function
in
in
JavaScript.
javascript
The
the
function
function
takes
takes
the
the
target
target
value
value
as
as
an
an
argument
argument
as
as
well
well
as
as
a
a
starting
starting
and
and
ending
ending
index.
index
First,
first
we
we
have
have
a
a
base
base
condition
condition
to
to
stop
stop
looping
looping
when
when
it
it
reaches
reaches
the
the
end
end
of
of
the
the
the
the
array,
array
at
at
which
which
point
point
we
we
know
know
the
the
target
target
is
is
not
not
in
in
the
the
array.
array
From
from
there,
there
we
we
compute
compute
the
the
middle
middle
index,
index
then
then
we
we
check
check
to
to
see
see
if
if
that
that
middle
middle
index
index
is
is
equal
equal
to
to
the
the
target,
target
at
at
which
which
point
point
we
we
can
can
return
return
because
because
we
we
found
found
the
the
element
element
we're
we're
looking
looking
for.
for
Otherwise,
otherwise
we
we
need
need
to
to
continue
continue
searching
searching
and
and
that's
that's
where
where
recursion
recursion
comes
comes
into
into
play.
play
If
if
the
the
middle
middle
value
value
is
is
greater
greater
than
than
the
the
target,
target
then
then
we'll
we'll
call
call
the
the
same
same
function,
function
but
but
this
this
time
time
we'll
we'll
put
put
the
the
ending
ending
index
index
at
at
the
the
middle.
middle
The
the
other
other
possibility
possibility
is
is
that
that
the
the
middle
middle
value
value
is
is
less
less
than
than
the
the
target,
target
in
in
which
which
case
case
we
we
can
can
also
also
to
to
recurse,
recurse
but
but
this
this
time,
time
we'll
we'll
put
put
the
the
starting
starting
index
index
in
in
the
the
middle.
middle
And
and
now
now
we
we
have
have
an
an
algorithm
algorithm
that
that
can
can
find
find
an
an
element
element
in
in
a
a
sorted
sorted
array
array
much
much
faster
faster
than
than
a
a
regular
regular
loop.
loop
If
if
you
you
wanna
wanna
see
see
more
more
videos
videos
about
about
algorithms
algorithms
on
on
this
this
channel.
channel
Hit
hit
the
the
like
like
button
button
and
and
let
let
me
me
know
know
in
in
the
the
comments.
comments
Thanks
thanks
for
for
watching
watching
and
and
I
i
will
will
see
see
you
you
in
in
the
the
next
next
one.
one
success
The speaker explains how to search for elements in a sorted array by using a for loop method. They then discuss a method to search for elements in a list by finding the middle index, which is used to determine the target. The algorithm can find elements much faster than a regular loop and is based on an iterative approach with a while loop or a denies loop.
Binary Search Algorithm in 100 Seconds
Binary search. An algorithm for finding an element in a sorted array by continuously chopping the search area in half. The first known implementation took place around 200 BC in ancient Babylon on clay tablets used for record keeping, but your brain has likely implemented binary search in everyday life without you even knowing it. Imagine looking for word like magic in the dictionary. You wouldn't flip every page until you get to m. Instead, you open it in the middle. If you overshoot to the q's, bus, then you go to the middle of that chunk. This time, you undershoot to the j's, so you do the same thing again, but in the other direction until finally you find the page you're looking more. Easy enough. Now it's time for your technical interview. You might be given a question, like, given a sorted array, write a function that returns for the index for the given element. The easy way to solve that problem is with a simple for loop. Just loop over every element in the array until you find the one you're looking before. That works, but you fail the interview because you need to go faster. A regular loop results in linear time complexity, but you can do better than that. Here's how we might map out a binary search on a whiteboard. But first, we need to figure out the middle index. Instead of starting at index 0, we start in the middle. If it's equal to the target, then we return that index. But if that element is greater than the target, then we know the target must be somewhere on the left. So we find the middle of that slice of the array. But if it's less than the target, then we know the target is somewhere on right, so we repeat the process for that slice of the array. The result is a much faster algorithm with logarithmic time complexity because it's able to divide and conquer. Now to implement the code, we could use an iterative approach with a while loop or a recursive function. Either way is acceptable, but let's define a recursive function in JavaScript. The function takes the target value as an argument as well as a starting and ending index. First, we have a base condition to stop looping when it reaches the end of the the array, at which point we know the target is not in the array. From there, we compute the middle index, then we check to see if that middle index is equal to the target, at which point we can return because we found the element we're looking for. Otherwise, we need to continue searching and that's where recursion comes into play. If the middle value is greater than the target, then we'll call the same function, but this time we'll put the ending index at the middle. The other possibility is that the middle value is less than the target, in which case we can also to recurse, but this time, we'll put the starting index in the middle. And now we have an algorithm that can find an element in a sorted array much faster than a regular loop. If you wanna see more videos about algorithms on this channel. Hit the like button and let me know in the comments. Thanks for watching and I will see you in the next one.
The speaker explains how to search for elements in a sorted array by using a for loop method. They then discuss a method to search for elements in a list by finding the middle index, which is used to determine the target. The algorithm can find elements much faster than a regular loop and is based on an iterative approach with a while loop or a denies loop.