From the course: Programming Foundations: Algorithms
Unlock the full course today
Join today to access over 24,800 courses taught by industry experts.
Binary search - Python Tutorial
From the course: Programming Foundations: Algorithms
Binary search
- [Instructor] Searching for data in an ordered or already sorted list is much more efficient than for an unordered list, and that's because we have a valuable piece of information about the list and the items in it, the fact that it is sorted. With a sorted list, we can perform a type of search that's called a binary search. So let's imagine that we had a list of sorted numbers and the number that we're searching for is this number right here, number 41. To perform the binary search, we start off with two indexes at the beginning and the end of the list, and then we calculate the midpoint of the list. Then we check to see if that value at the midpoint is the value that we want. And if it is, then great we return that index. If the number at that index is less than the one that we are searching for, then we know that we could ignore all the numbers below that index. And conversely, if that number is larger than the one…