Feb 28
The code we did in class is attached.
Reminder of what we discussed last time:
Sequential search: O(n) , works for either indexed or linked list, on unsorted list.
Discuss and write code in class for
binary search
- assumes ordered index-accessible list
- compare search with middle term ... recursively repeat on half that may have it
- 1st example of "divide and conquer" strategy
- O(log n)
This is a case where "preparing" the data (i.e. sorting) is a good strategy when the algorithm to be run (i.e. the search) will be done many times. If we need to search an unordered list only once, binary search would need to include the sort time (typically O(n log(n))) which would make it slower than sequential search.
hash table (i.e. python dict)
- a data structure with O(1) lookup (!)
- using a trick: the "hash function"
The central idea is to use the search key to get a "hash index" through a pseudo-random function, and then jump to that index as a place to put the data. The complication is that more than one key may hash to the same value ... an issue which can be dealt with in various ways.
Both of these discussed in the "searching and sorting" chapter in the text.