Path: blob/master/python/algorithms/search_sort.ipynb
1480 views
Searching, Hashing, Sorting
Following the online book, Problem Solving with Algorithms and Data Structures. Chapter 6 introduces methods for searching and sorting numbers.
Searching
We can take advantage of a ordered list by doing a binary search. We start by searching in the middle, if it is not the item that we're searching for, we can use the ordered nature of the list to eliminate half of the remaining items.
Keep in mind that this approach requires sorting the list, which may not be ideal if we're simply going to search for 1 number on the very large list (since we have to first sort the list, which is not a cheap operation).
Hashing
Quick notes:
Dictionary or map that let us stor key value pairs is typically implemented using hash table.
An element that we wish to add is converted into an integer by using a hash function.
hash = hash_function(key)
Th resulting hash is independent of the underlying array size, and it is then reduced to an index by using the modulo operator.
index = hash % array_size
Python has a built in hash
function that can calculate has hash values for arbitrarily large objects in a fast manner. Hash function that is used for dictionary or maps should be deterministic so we can look up the values afterwards, fast to compute, else the overhead of hashing would offset the benefit it brings for fast lookup, and uniform distributed, this one is related to avoiding hash collision.
Hash collision happens when two different inputs produce the same hash value. To avoid this a well implemented hash table should be able to resolve hash collision, common techniques include linear probing and separate chaining. Also wehn our hash table is almost saturated (the number of elements is close to the array size we've defined), a.k.a the load factor is larger than some portion, then it should be able to dynamically resize the hash table to maintain our dictionary and map's performance.
Real Python: Build a Hash Table in Python With TDD has a much in depth introduction to this topic.
Sorting
Merge Sort
Quick Sort