Searching a set/list of items #### Unsorted items [draw big vertical array and linked list] Worst case: unsorted list of unknown items. Must linearly search each item and test for equals(). What is order? O(n). Why? #### Sorted [draw big vertical sorted array; Antelope, Cow, Dog, Hippo, Zebra] What if you know the list items are sorted (according to some measure)? What is the theoretically fastest sort you can do? O(log_2(n)). Why? Because each time you cut the search space in half. You may cut n in half exactly log_2(n) times. #### How can we reduce search time by a multiple factor? [draw regions in big array] Divide the whole list into "regions" or "buckets" and then search linearly in the region. If there are r regions, your search is how fast? O(n/r). Bigger the r, the faster the search. How do divide into regions? See below. #### What if the items are integers? [draw a bit array with indices] Can just turn on bit i if i is in list. How fast is this? O(1). Why? Can you go faster? Not very space efficient. Can use bitsets, but still very sparse. #### What if you need quasi-O(1) search for non-integers? Easy: turn the non-integers into integers first and then proceed as before. The conversion of non-integers into integers is called computing a "hash code". It essentially converts a single big list into multiple smaller lists (regions or buckets). [draw smaller array with arrays out the edge] Fixed arrays for buckets are not very space efficient, so use a linked list. [draw smaller array with list emanating] #### Map vs List How is a Map different than a list? List item is key that is mapped to a value like map(key) = value as in math. Wrapper: object that holds key/value pair. Might incorporate the linked list references into the object to implement hashtable buckets.