Learn Binary Search in 10 minutes 🪓

00:10:04
https://www.youtube.com/watch?v=xrMppTpoqdw

Résumé

TLDRThe video provides a detailed explanation of the binary search algorithm, which is used in computer science to locate the position of a target value within a sorted array or collection. The binary search algorithm functions by comparing the target value with the middle element of the array, determining whether to continue the search in the left or right half of the array, thus eliminating half of the elements in each step. The algorithm is significantly efficient for large datasets due to its logarithmic time complexity, O(log n), in contrast to linear searches which have a time complexity of O(n). This makes binary search significantly faster for large collections of data, especially beyond 1 million elements, where it can narrow down the search to find the target value in about 20 steps as demonstrated in the video. The video further demonstrates how to use built-in binary search methods in programming, alongside writing custom functions for practice, showing both iterative and recursive implementations. Overall, binary search is recommended for large, sorted datasets, where it consistently performs better than a linear approach to searching.

A retenir

  • 🧠 Understand the binary search algorithm for sorted arrays.
  • 🔍 Binary search compares the target to the middle value, adjusting the search scope.
  • ▶️ Efficient for large datasets with complexity O(log n).
  • 🆚 Outperforms linear search for sizeable arrays.
  • 🚫 Requires sorted data for effective functionality.
  • ✍️ Demonstrated via both built-in methods and custom functions.
  • 🔄 Illustrated both iterative and recursive approaches.
  • ⚙️ Useful in programming when dealing with large sorted data.
  • 📊 Demonstrated its efficiency dramatically decreases the number of checks needed.
  • ➡️ Requires random access data structures for implementation.

Chronologie

  • 00:00:00 - 00:05:00

    The video introduces binary search, an algorithm in computer science used to find the position of a target value within a sorted array by repeatedly dividing the search interval in half. For binary search to work, the array must be sorted. The process involves checking the middle element and deciding which half of the array to discard, repeating this until the target is found. Binary search is efficient for large datasets, with a time complexity of O(log n), making it faster compared to a linear search.

  • 00:05:00 - 00:10:04

    The speaker demonstrates binary search using a real-life example with a built-in method in arrays and then by manually coding the function for practice. The video highlights how binary search efficiently reduces the number of elements to check regardless of a large dataset, showing significant performance improvement over linear search methods. The conclusion reaffirms the efficiency of binary search in finding a target within a sorted array while sharing the code and encouraging viewers to like, comment, and subscribe.

Carte mentale

Mind Map

Questions fréquemment posées

  • What is a binary search algorithm?

    A binary search algorithm is a search algorithm that finds the position of a target value within a sorted array by repeatedly dividing the search interval in half.

  • Why is sorting necessary for binary search?

    Sorting is necessary because binary search works by halving the search space, which depends on the order of elements; therefore, the list must be sorted for it to function properly.

  • How does binary search work?

    Binary search works by comparing the target value to the middle element of a sorted array. If they are not equal, it eliminates half of the search space depending on whether the target is greater or less than the middle element.

  • What is the time complexity of binary search?

    The time complexity of a binary search is O(log n), making it very efficient for large datasets.

  • How does binary search compare to linear search?

    Binary search is more efficient than linear search, especially for large datasets, because it eliminates half of the elements in each step, while linear search goes through each element one by one.

  • What data structures can binary search be used on?

    Binary search can be used on arrays and any other data structures that allow random access to the elements like lists, as long as they are sorted.

  • What happens if the target value is not found in binary search?

    If the target value is not found, binary search typically returns a sentinel value, like -1, indicating that the element is not in the array.

  • Can binary search be implemented recursively?

    Yes, binary search can be implemented both iteratively and recursively, with the same time complexity of O(log n).

  • When is binary search not efficient?

    Binary search is not efficient for small datasets because the overhead of calculations outweighs the benefit of halving the search space.

Voir plus de résumés vidéo

Accédez instantanément à des résumés vidéo gratuits sur YouTube grâce à l'IA !
Sous-titres
en
Défilement automatique:
  • 00:00:00
    hey everyone it's you bro hope you're
  • 00:00:01
    doing well and in this video i'm going
  • 00:00:03
    to explain
  • 00:00:04
    the binary search algorithm in computer
  • 00:00:07
    science
  • 00:00:07
    so sit back relax and enjoy the show
  • 00:00:12
    all right binary search it's a search
  • 00:00:15
    algorithm that finds
  • 00:00:16
    the position of a target value within a
  • 00:00:19
    sorted array or other collection
  • 00:00:21
    in order for a binary search to work
  • 00:00:23
    whatever we're searching through
  • 00:00:24
    it needs to be sorted and basically what
  • 00:00:27
    we do is we take
  • 00:00:28
    half of the array and eliminate it
  • 00:00:30
    during each step and we will whittle
  • 00:00:31
    down our collection until there is only
  • 00:00:33
    one element remaining
  • 00:00:35
    in this example i have an array of 11
  • 00:00:37
    elements each element contains a letter
  • 00:00:39
    and they're all sorted alphabetically
  • 00:00:41
    let's say we are looking for the letter
  • 00:00:43
    h and i need the index
  • 00:00:45
    what we would do with a binary search is
  • 00:00:47
    that we always begin in the middle
  • 00:00:48
    we first check to see if our target
  • 00:00:51
    value is equal to this middle value
  • 00:00:53
    if these are equal then we can return
  • 00:00:55
    this index but odds are they're probably
  • 00:00:57
    not
  • 00:00:58
    going to be equal on the very first turn
  • 00:01:00
    the very first step
  • 00:01:01
    so these are not equal then we will
  • 00:01:03
    check to see if our target value
  • 00:01:06
    is greater than or less than this middle
  • 00:01:08
    value
  • 00:01:09
    since h is greater than f we can
  • 00:01:11
    disregard
  • 00:01:12
    this entire first half of our array
  • 00:01:14
    because since this is sorted
  • 00:01:16
    our target value could not possibly be
  • 00:01:18
    within this first portion
  • 00:01:20
    and then we begin step two or phase two
  • 00:01:23
    it's the same process as before
  • 00:01:25
    so again we begin in the middle check to
  • 00:01:27
    see if our target value
  • 00:01:29
    is equal to the middle value there or
  • 00:01:31
    not check to see if our target value
  • 00:01:33
    is greater than or less than the middle
  • 00:01:36
    value
  • 00:01:36
    this time h is less than i we would
  • 00:01:39
    delete the
  • 00:01:40
    second half of this portion we're not
  • 00:01:42
    actually deleting these values we're
  • 00:01:43
    disregarding them
  • 00:01:44
    and then we can move on to step three
  • 00:01:46
    we're repeating the same process as
  • 00:01:48
    before
  • 00:01:49
    and this time these elements divide
  • 00:01:51
    evenly so we would just round down and
  • 00:01:53
    begin
  • 00:01:54
    and say that this is the middle so h is
  • 00:01:56
    greater than
  • 00:01:57
    g we would disregard this element and we
  • 00:02:00
    only have h remaining so we would return
  • 00:02:03
    this index because these values are
  • 00:02:05
    equal and that's a binary search
  • 00:02:08
    now a binary search isn't too efficient
  • 00:02:10
    when working with
  • 00:02:11
    small data sets however if you're
  • 00:02:12
    working with a large data set like
  • 00:02:15
    1 million elements well then a binary
  • 00:02:18
    search is actually fantastic
  • 00:02:20
    because we're eliminating half of the
  • 00:02:21
    elements we are searching through
  • 00:02:23
    during each phase or turn so if we had a
  • 00:02:26
    million elements
  • 00:02:27
    after the first phase we can already
  • 00:02:29
    disregard like half a million elements
  • 00:02:32
    and then we just repeat the process
  • 00:02:33
    until there's only one left
  • 00:02:35
    so if this was an iterative approach we
  • 00:02:37
    would need to search through these
  • 00:02:39
    linearly beginning with
  • 00:02:40
    you know index zero and going all the
  • 00:02:42
    way to a million so a binary search is
  • 00:02:45
    fantastic with large data sets
  • 00:02:47
    the runtime complexity of a binary
  • 00:02:49
    search is o of log
  • 00:02:51
    n the larger the data set a binary
  • 00:02:53
    search becomes more and more efficient
  • 00:02:55
    compared to other search algorithms
  • 00:02:57
    alright let's perform a binary search in
  • 00:02:59
    real life now we'll use the built-in
  • 00:03:02
    binary search method of arrays to begin
  • 00:03:04
    with and then later on we'll build our
  • 00:03:06
    own binary search function
  • 00:03:07
    so we'll need an array to work with
  • 00:03:09
    let's say we have an array of integers
  • 00:03:11
    named array
  • 00:03:12
    int array and the size of this array
  • 00:03:15
    will be 100
  • 00:03:16
    we'll increase the size later for
  • 00:03:17
    demonstration purposes
  • 00:03:19
    and we'll need a target value that we're
  • 00:03:21
    searching for i'll just name this target
  • 00:03:23
    and target equals what about 42 we'll
  • 00:03:25
    search for the number 42 and we'll need
  • 00:03:27
    to
  • 00:03:28
    populate our array so we can do so using
  • 00:03:30
    a for loop int
  • 00:03:32
    i equals zero we will continue this for
  • 00:03:34
    loop as long
  • 00:03:35
    as i is less than array dot length
  • 00:03:39
    and increment i by one during each
  • 00:03:42
    iteration
  • 00:03:43
    then we will fill array at index i
  • 00:03:46
    with whatever i is our index
  • 00:03:49
    okay so the cheap way of using a binary
  • 00:03:53
    search
  • 00:03:53
    is to use the built-in binary search
  • 00:03:56
    method of arrays
  • 00:03:57
    let's say int index
  • 00:04:01
    equals arrays dot
  • 00:04:04
    binary search and taking a look at this
  • 00:04:08
    binary search method
  • 00:04:09
    there's two arguments that we have to
  • 00:04:11
    pass in an array
  • 00:04:13
    and whatever we're searching for so we
  • 00:04:15
    will pass in
  • 00:04:16
    our array and our target then return the
  • 00:04:19
    index
  • 00:04:20
    and let's display that so let's check to
  • 00:04:23
    see
  • 00:04:23
    if our index is equal to negative one
  • 00:04:28
    if our target is not found then that
  • 00:04:30
    means
  • 00:04:31
    negative one will be returned from our
  • 00:04:33
    binary search method
  • 00:04:34
    so let's print something
  • 00:04:36
    system.out.printline
  • 00:04:38
    uh what about element not found
  • 00:04:42
    actually better yet target not found let
  • 00:04:44
    me change that
  • 00:04:45
    target plus not found
  • 00:04:48
    okay then else
  • 00:04:52
    else we will display
  • 00:04:54
    system.out.printline
  • 00:04:57
    element found at
  • 00:05:01
    colon space plus index
  • 00:05:05
    all right let's try it okay element
  • 00:05:08
    found at 42
  • 00:05:10
    cool now let's create our own binary
  • 00:05:12
    search function for practice
  • 00:05:13
    i'll turn this line into a comment copy
  • 00:05:16
    it
  • 00:05:17
    paste it and get rid of this erase
  • 00:05:18
    portion
  • 00:05:20
    okay then i'm just going to use a
  • 00:05:22
    shortcut to generate this function
  • 00:05:25
    okay so private static int binary search
  • 00:05:28
    there are two parameters an array of
  • 00:05:31
    integers named array
  • 00:05:32
    and int target so we'll return negative
  • 00:05:35
    one
  • 00:05:36
    that acts as a sentinel value negative 1
  • 00:05:38
    means that
  • 00:05:39
    the value was not found now what we'll
  • 00:05:42
    need at this point
  • 00:05:43
    is the beginning and ending index of our
  • 00:05:45
    array so let's say
  • 00:05:47
    int low will be the beginning and int
  • 00:05:50
    high
  • 00:05:50
    is the end array dot length
  • 00:05:54
    minus one so we have a low index and
  • 00:05:57
    high index and we'll create a while loop
  • 00:06:01
    while low is less than or equal to high
  • 00:06:05
    we'll continue this while loop
  • 00:06:06
    and keep on searching through our array
  • 00:06:09
    so first we need
  • 00:06:10
    the middle index int middle
  • 00:06:13
    and here's the formula for that low plus
  • 00:06:17
    high minus low divided by two
  • 00:06:21
    so we have our middle index we will take
  • 00:06:25
    int value equals our array
  • 00:06:29
    at index of middle so this will extract
  • 00:06:32
    that value found within
  • 00:06:34
    this element okay so this portion is
  • 00:06:37
    optional i'm just going to display
  • 00:06:38
    whatever this value is
  • 00:06:40
    so we can count the amount of steps it's
  • 00:06:41
    going to take to find a value
  • 00:06:43
    so let's say middle colon space whatever
  • 00:06:47
    this value is this line of code is
  • 00:06:49
    optional i'm just doing this for
  • 00:06:50
    educational purposes
  • 00:06:52
    okay now we need to check to see if our
  • 00:06:54
    value is
  • 00:06:55
    less than or greater than our target or
  • 00:06:58
    equal to
  • 00:07:00
    if value
  • 00:07:03
    is less than our target
  • 00:07:07
    low equals middle plus
  • 00:07:11
    one and actually i'm going to get rid of
  • 00:07:13
    these curly braces
  • 00:07:14
    if you have an if statement and you only
  • 00:07:16
    have like one line of code you don't
  • 00:07:17
    really need
  • 00:07:18
    the curly braces i'm just doing this so
  • 00:07:20
    it's easier to read
  • 00:07:22
    okay else if
  • 00:07:26
    value is greater than target
  • 00:07:30
    we will set our height index high equals
  • 00:07:34
    middle minus one
  • 00:07:37
    else that means we have found our target
  • 00:07:39
    else
  • 00:07:40
    return middle
  • 00:07:43
    so this means that target is found
  • 00:07:48
    and by returning negative one that means
  • 00:07:50
    target
  • 00:07:51
    not found and that is our binary search
  • 00:07:54
    function
  • 00:07:54
    let's try it okay element found at 42
  • 00:07:58
    so it took us let's see one two three
  • 00:08:01
    four four steps to find the number 42
  • 00:08:04
    within this array of 100 elements
  • 00:08:06
    now let's increase the size because
  • 00:08:08
    binary searches do extremely well
  • 00:08:10
    with large data sets so let's say we
  • 00:08:13
    have
  • 00:08:13
    1 million elements and let's change this
  • 00:08:15
    target
  • 00:08:16
    what about 700
  • 00:08:19
    7 thousands whatever that number is
  • 00:08:23
    okay so let's search for it and let's
  • 00:08:25
    count the steps
  • 00:08:27
    uh so there's quite a number of steps
  • 00:08:29
    here but let's count them
  • 00:08:31
    1 2 3 4 5 6 7
  • 00:08:34
    8 9 10 11 12 13 14 15
  • 00:08:38
    16 17 18 19 20 20 steps
  • 00:08:41
    now imagine if we took a linear approach
  • 00:08:43
    where we began
  • 00:08:44
    at index zero and looped through this
  • 00:08:46
    entire array
  • 00:08:47
    looking for this index and in that case
  • 00:08:50
    looping through an array would have a
  • 00:08:51
    runtime complexity of o event
  • 00:08:53
    to find this number it's going to take
  • 00:08:55
    over 700 000 steps because we're
  • 00:08:58
    iterating
  • 00:08:58
    once for each element within this array
  • 00:09:01
    compared to a binary search where it
  • 00:09:02
    only took 20 steps
  • 00:09:04
    well then in conclusion a binary search
  • 00:09:06
    is a search
  • 00:09:07
    algorithm that finds the position of a
  • 00:09:10
    target value within
  • 00:09:11
    a sorted array half of the array is
  • 00:09:14
    eliminated during each step or phase
  • 00:09:17
    so that's a binary search algorithm if
  • 00:09:19
    you would like a copy of all this code
  • 00:09:21
    of course i will post this to the
  • 00:09:22
    comment section down below
  • 00:09:24
    and that is the binary search algorithm
  • 00:09:26
    in computer science
  • 00:09:29
    hey you yeah i'm talking to you if you
  • 00:09:31
    learned something new
  • 00:09:32
    then help me help you in three easy
  • 00:09:35
    steps by smashing that like button
  • 00:09:37
    drop a comment down below and subscribe
  • 00:09:39
    if you'd like to become a fellow bro
  • 00:09:50
    [Music]
  • 00:10:03
    you
Tags
  • binary search
  • algorithm
  • sorted array
  • computer science
  • complexity
  • searching
  • data structures
  • iteration
  • efficiency