Java Lecture 10
Searching and Sorting
Reading
- Arrays were Ch 6, up to p228
- Graphics: Ch 7, pp 252-254 (Color: 255-256)
- Today: Ch 13, but 13.2 before 13.1, and not p480 (Hashing)
- Soon: Ch 12
- Hopefully: Ch 2.4-2.6
Searching
- Previously, we went through how to do a linear search through
an array for an value
- just start at the beginning, and search through until you find it
- 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
- In the best case, this makes you search only one value (the
first one is it)
- In the average case, you can expect to search through about
1/2 of the N values
- Linear search does not make any assumptions about where the values
are in the array
- What if you could? What if you know, ahead of time, that the values
in the arrray are sorted?
- Sorted means that the values always increase as the index in the array
gets larger
- Well, it could mean they always decrease (a kind of reverse sorted
order)
- With a sorted array, can we do better than linear search?
- The answer, thankfully, is yes. One solution is binary search.
- Binary Search
- 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
- Select the middle-point of the array
- If the value at this point is the value you are looking for, stop with
a "found it" result
- 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.
- Consider this new 1/2-array your whole array, and go to step 1
- 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.
- How does it perform?
- In the best case, it makes you search one value (the first one,
in the middle, is the one you are looking for)
- In the worst case, it makes you search log(N) values -- where
log is a log-base-two
- In the average case, it makes you search log(N) values, just
like the worst case
- But, this is hugely better than linear search!!
- Computer scientists really like logarithmic behavior!
- What does this mean?
- For 100 values, binary search will search at most 7 values
- For 10000 values, binary search will search at most 14 values
- For a million values, binary search will search at most 20 values
- For a trillion values, binary search will search at most 40 values
- 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.