Fireship · @fireship
Binary Search Algorithm in 100 Seconds
Uploaded March 23, 2022 · Length 2:20
Summary
binary search algorithm explained in 100 seconds with example and implementation in javascript
- binary search
- algorithm
- javascript
- sorting
- recursion
- time complexity
Transcript preview
The first 2 minutes of the transcript. Select a timestamp to watch that moment on YouTube.
- 0:00
binary search an algorithm for finding
- 0:02
an element in a sorted array by
- 0:04
continuously chopping the search area in
- 0:06
half the first known implementation took
- 0:08
place around 200 bc and ancient babylon
- 0:10
on clay tablets used for record keeping
- 0:13
but your brain has likely implemented
- 0:14
binary search in everyday life without
- 0:16
you even knowing it imagine looking for
- 0:18
a word like magic in the dictionary you
- 0:20
wouldn't flip every page until you get
- 0:21
to m instead you open it in the middle
- 0:24
if you overshoot to the cues then you go
- 0:26
to the middle of that chunk this time
- 0:27
you undershoot to the js so you do the
- 0:29
same thing again but in the other
- 0:31
direction until finally you find the
- 0:32
page you're looking for easy enough now
- 0:34
it's time for your technical interview
- 0:36
you might be given a question like given
- 0:37
a sorted array write a function that
- 0:39
returns the index for the given element
- 0:41
the easy way to solve that problem is
- 0:43
with a simple for loop just loop over
- 0:45
every element in the array until you
- 0:47
find the one you're looking for that
- 0:48
works but you failed the interview
- 0:50
because you need to go faster a regular
- 0:51
loop results in linear time complexity
- 0:54
but you can do better than that here's
- 0:55
how we might map out a binary search on
- 0:56
a whiteboard first we need to figure out
- 0:58
the middle index instead of starting at
- 1:00
index 0 we start in the middle if it's
- 1:02
equal to the target then we return that
- 1:04
index but if that element is greater
- 1:06
than the target we know the target must
- 1:07
be somewhere on the left so we find the
- 1:09
middle of that slice of the array but if
- 1:11
it's less than the target then we know
- 1:12
the target is somewhere on the right so
- 1:14
we repeat the process for that slice of
- 1:15
the array the result is a much faster
- 1:17
algorithm with logarithmic time
- 1:19
complexity because it's able to divide
- 1:21
and conquer now to implement the code we
- 1:23
could use an iterative approach with a
- 1:24
while loop or a recursive function
- 1:26
either way is acceptable but let's
- 1:28
define a recursive function in
- 1:30
javascript the function takes the target
- 1:32
value as an argument as well as a
- 1:33
starting and ending index first we have
- 1:35
a base condition to stop looping when it
- 1:37
reaches the end of the array at which
- 1:39
point we know the target is not in the
- 1:41
array from there we compute the middle
- 1:43
index then we check to see if that
- 1:44
middle index is equal to the target at
- 1:46
which point we can return because we
- 1:48
found the element we're looking for
- 1:49
otherwise we need to continue searching
- 1:51
and that's where recursion comes into
- 1:52
play if the middle value is greater than
- 1:54
the target then we'll call the same
- 1:56
function but this time we'll put the
- 1:57
ending index at the middle the other
- 1:59
possibility is that the middle value is


