Check out a free preview of the full Complete Intro to Computer Science course

The "Binary Search" Lesson is part of the full, Complete Intro to Computer Science course featured in this preview video. Here's what you'd learn in this lesson:

Brian discusses searching for a particular element in an array using a binary search algorithm to keep cutting the array in half until the correct value is found. Unlike a linear search, a binary search method will only work if the array is already sorted. A student's question regarding if a binary search is best done recursively is also covered in this segment.


Transcript from the "Binary Search" Lesson

>> I imagine many of you kind of like conceptually already understand this, even if you haven't put a name to necessarily this algorithm. But it's useful for you to just kind of put a name to it right at once make sure you understand it. The act of searching is kind of similar to sorting and the fact that you are comparing numbers one to each other.

But in this particular case, we're not trying to get all the numbers in one particular order we're actually just trying to find one particular element in the array. If you're running a search on a array that's not sorted, the only kind of search you can really do is linear right which is you just write a for loop or a while loop and say like is this it?

Is this it? Is this it? And you just kinda go pick for that that just checks every single element in the array. However, if you have an array that's already sorted, you can use binary search. And I imagine if you're old enough to remember phonebooks, [LAUGH] Which may not be everyone that's watching this video.

The way that you search in a phonebook if you're looking for I don't know, hold in the phonebook, you open it to the middle, you're gonna land on like M or N. And then you're gonna split the difference and go halfway between M and N, right? And then you're just gonna keep going half and half and a half until eventually learn on the Hs than the HOs and the HLs, right and then eventually we'll find holes right.

So this algorithm of splitting the array in half and then half and then half and in half until eventually you land on the exact correct element it's called the binary search, right? So, imagine we have this array here, again, binary search works only if it is sorted, right, and we're looking for 12.

So what you do you always you just start in the middle, right? So the middle is 19, you say is 19 equal to 12? No, it's smaller and then you go to the left and you split the difference there. So you look in the middle of the smaller half.

So between 19 and 0, that middle is going to be ten, right? You ask this ten equal to 12? The answer no it's not. So you go larger, right? And you go right and you look in the middle of the larger half, which is between, in this case just between 10 and 12, right?

And then you just look there, which is going to be 12. And then you ask as well be equals to 12, yes, you found the element that makes sense. Just keeps cutting the array in half until eventually you find that the item that you're looking for. One of the questions you might ask is like, if it's of length 3, do I go to the smaller side or the larger side?

Doesn't matter, just pick 1. I think in this particular case I went to the smaller side, which is why we looked at 10 first instead of 12. Any questions about the algorithm of binary search? Is this best done recursively? No, I would say this is much better done with a loop than it is.

Yeah, I guess you could totally do this with recursively. I probably would do this with had loop. Let me just make sure that I'm not telling you lies here. Search. Yep, either way though, I mean, honestly, binary search, even on extremely large arrays. Is really effective. So you probably would be fine to do it in recursive ways as well, but my solution is iterative.

What is the O of this? Well, we're not looking at every element in the array, right? So it's not n it's gonna be less than n because we're just starting in the middle and then we're going to the side that we need to. So we're not looking at every element in the array.

So less than n, we do have to look at some elements in the array. And it also we get economies of scale if we have extremely large datasets. So that puts us at login, right? So binary search as an algorithm is time complexity, login. And if you don't create any additional data in the process, so it's constant space.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now