Java Lecture 10

Searching and Sorting

Reading

  1. Arrays were Ch 6, up to p228
  2. Graphics: Ch 7, pp 252-254 (Color: 255-256)
  3. Today: Ch 13, but 13.2 before 13.1, and not p480 (Hashing)
  4. Soon: Ch 12
  5. Hopefully: Ch 2.4-2.6

Searching

  1. Previously, we went through how to do a linear search through an array for an value
    1. just start at the beginning, and search through until you find it
  2. In the worst case, this makes you search all N values in the array, if the one you are looking for is the last one, or is not found at all
  3. In the best case, this makes you search only one value (the first one is it)
  4. In the average case, you can expect to search through about 1/2 of the N values
  5. Linear search does not make any assumptions about where the values are in the array
  6. What if you could? What if you know, ahead of time, that the values in the arrray are sorted?
    1. Sorted means that the values always increase as the index in the array gets larger
    2. Well, it could mean they always decrease (a kind of reverse sorted order)
  7. With a sorted array, can we do better than linear search?
  8. The answer, thankfully, is yes. One solution is binary search.
  9. Binary Search
    1. If the size of the array is 1, and its one value is not the one you are looking for, stop with a "not found" result
    2. Select the middle-point of the array
    3. If the value at this point is the value you are looking for, stop with a "found it" result
    4. If the value is less than the value in the array at this point, "throw away" the top half of the array. If the value is greater than that in the array, "throw away" the bottom half.
    5. Consider this new 1/2-array your whole array, and go to step 1
  10. Binary search is an example of a divide-and-conquer algorithm -- at each step it divides the problem into halves, and throws away a whole half.
  11. How does it perform?
  12. In the best case, it makes you search one value (the first one, in the middle, is the one you are looking for)
  13. In the worst case, it makes you search log(N) values -- where log is a log-base-two
  14. In the average case, it makes you search log(N) values, just like the worst case
  15. But, this is hugely better than linear search!!
    1. Computer scientists really like logarithmic behavior!
  16. What does this mean?
    1. For 100 values, binary search will search at most 7 values
    2. For 10000 values, binary search will search at most 14 values
    3. For a million values, binary search will search at most 20 values
    4. For a trillion values, binary search will search at most 40 values
  17. So, linear search would be looking through 500 billion values, while binary search looked at 40!

Of course, binary search depends on the array being ordered. If it is not, and its usually not, then we need to sort it ourselves. We will next look at methods of sorting.