Fireship · @fireship

Binary Search Algorithm in 100 Seconds

Uploaded March 23, 2022 · Length 2:20

Binary Search Algorithm in 100 Seconds

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.

  1. 0:00

    binary search an algorithm for finding

  2. 0:02

    an element in a sorted array by

  3. 0:04

    continuously chopping the search area in

  4. 0:06

    half the first known implementation took

  5. 0:08

    place around 200 bc and ancient babylon

  6. 0:10

    on clay tablets used for record keeping

  7. 0:13

    but your brain has likely implemented

  8. 0:14

    binary search in everyday life without

  9. 0:16

    you even knowing it imagine looking for

  10. 0:18

    a word like magic in the dictionary you

  11. 0:20

    wouldn't flip every page until you get

  12. 0:21

    to m instead you open it in the middle

  13. 0:24

    if you overshoot to the cues then you go

  14. 0:26

    to the middle of that chunk this time

  15. 0:27

    you undershoot to the js so you do the

  16. 0:29

    same thing again but in the other

  17. 0:31

    direction until finally you find the

  18. 0:32

    page you're looking for easy enough now

  19. 0:34

    it's time for your technical interview

  20. 0:36

    you might be given a question like given

  21. 0:37

    a sorted array write a function that

  22. 0:39

    returns the index for the given element

  23. 0:41

    the easy way to solve that problem is

  24. 0:43

    with a simple for loop just loop over

  25. 0:45

    every element in the array until you

  26. 0:47

    find the one you're looking for that

  27. 0:48

    works but you failed the interview

  28. 0:50

    because you need to go faster a regular

  29. 0:51

    loop results in linear time complexity

  30. 0:54

    but you can do better than that here's

  31. 0:55

    how we might map out a binary search on

  32. 0:56

    a whiteboard first we need to figure out

  33. 0:58

    the middle index instead of starting at

  34. 1:00

    index 0 we start in the middle if it's

  35. 1:02

    equal to the target then we return that

  36. 1:04

    index but if that element is greater

  37. 1:06

    than the target we know the target must

  38. 1:07

    be somewhere on the left so we find the

  39. 1:09

    middle of that slice of the array but if

  40. 1:11

    it's less than the target then we know

  41. 1:12

    the target is somewhere on the right so

  42. 1:14

    we repeat the process for that slice of

  43. 1:15

    the array the result is a much faster

  44. 1:17

    algorithm with logarithmic time

  45. 1:19

    complexity because it's able to divide

  46. 1:21

    and conquer now to implement the code we

  47. 1:23

    could use an iterative approach with a

  48. 1:24

    while loop or a recursive function

  49. 1:26

    either way is acceptable but let's

  50. 1:28

    define a recursive function in

  51. 1:30

    javascript the function takes the target

  52. 1:32

    value as an argument as well as a

  53. 1:33

    starting and ending index first we have

  54. 1:35

    a base condition to stop looping when it

  55. 1:37

    reaches the end of the array at which

  56. 1:39

    point we know the target is not in the

  57. 1:41

    array from there we compute the middle

  58. 1:43

    index then we check to see if that

  59. 1:44

    middle index is equal to the target at

  60. 1:46

    which point we can return because we

  61. 1:48

    found the element we're looking for

  62. 1:49

    otherwise we need to continue searching

  63. 1:51

    and that's where recursion comes into

  64. 1:52

    play if the middle value is greater than

  65. 1:54

    the target then we'll call the same

  66. 1:56

    function but this time we'll put the

  67. 1:57

    ending index at the middle the other

  68. 1:59

    possibility is that the middle value is