Check out a free preview of the full A Practical Guide to Algorithms with JavaScript course:
The "Binary Search" Lesson is part of the full, A Practical Guide to Algorithms with JavaScript course featured in this preview video. Here's what you'd learn in this lesson:

## Bianca illustrates binary search, which allows a search of a sorted array by repeatedly splitting the array in half. Binary search is fast Since with each iteration half of the array is determined to be undesired, instead of just one wrong value.

Get Free Access Now

Transcript from the "Binary Search" Lesson

[00:00:00]
>> Bianca Gandolfo: So we're gonna talk about binary search. Again, binary search is our search where we are expecting our array to be sorted. And we're going to break our list in half every time, and we're gonna say, is our value that we're searching for less than, or greater than, this current location?

[00:00:17] So if it's less, we're gonna go to the left, if it's greater than, we're gonna go to the right. All right, what does this look like in reality? All right, so we could do the call stack game with this, that would be a fun one. And so we just called,

[00:00:38]
>> Bianca Gandolfo: Our binarySearch with these values.
>> Bianca Gandolfo: I'm gonna keep them up here for reference. So we're initializing a minimum, our maximum is list.length -1, which is 1, 2, 3, 4, 5, which is 4. Initializing a guess that's gonna be empty. So while the minimum is less than max, so while we aren't looping outside of the bounds of our function.

[00:01:06] We are going to grab the center, so our guess is, let's just start in the middle. And for, and this is common for a divide and conquer algorithm, we start in the middle. But if you take a look at quick sort, which we won't have time to cover today, It's not always gonna be in the middle.

[00:01:28] Sometimes you'll pick a point to do a divide and conquer at the end in the case of quick sort, but there's different implementations. However, you can kind of reason that if it's divide and conquer, you're probably gonna split it in the middle, although sometimes that's not the case.

[00:01:42] Okay, so is min less than or equal to the max, sure is, and then now our guess is going to be the second index. Okay, so did we find it, 0, 1, 2, no, we didn't. So we're gonna go into this else, is 7 less than 90? Yes, so we're going to increment our min now to 1.

[00:02:10] And then we are going to loop back into our while. So is 1 less than or equal to max, sure is. So now we are going to map that floor 5 divided by 2. So we're kind of narrowing the,
>> Bianca Gandolfo: Range that we're looking in. Okay, so then that is going to be 2, did we find it?

[00:02:49] No, otherwise we are going to, so is 7 less than 90? Yes, then we increment it again, so now this is 2, okay. Then 2+4 is 6, so now this is 3, so it's 0, 1, 2, 3, and then we find it, okay. Then we find it and we return it, we did it, there it is.

[00:03:17]