September 2017
Intermediate to advanced
240 pages
5h 57m
English
The binary search algorithm is a simple way to find an item in an already sorted list of items. It’s easily described as taking a sorted list and continually partitioning it into halves until you either find it or you run out. If you did the complete Exercise 20, then this exercise should be relatively easy.
If we wanted to find a number X in an already sorted list of numbers we would do this:
1. Get the number in the middle of the list (M) and compare it to X.
2. If X == M, you’re done.
3. If X > M, then find the middle of M+1 to the end of the list.
4. If X < M, then find the middle of M-1 to the beginning of the list.
5. Repeat, until either you find X or you have only 1 element left.
This works for anything that ...
Read now
Unlock full access