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