In the last chapter every vertex of a graph had a name: Dhaka, Khulna, Sylhet. To store the graph we gave each name a number and kept its list of neighbours in an array at that position. We skipped a small question on the way. When the program is handed the word "Khulna", how does it find the number? Reading through the names one by one works for eight of them. It does not work for a mobile-banking app with 100,000 accounts, where you type an 8-digit account number and the balance appears before you have lifted your finger.
Let's count what "reading through" costs. If the accounts sit in no particular order, finding yours takes 50,000 comparisons on average. If they are sorted by account number, binary search needs about 16, which is far better, but the list must stay sorted while new accounts arrive all day. Hashing does the job in about 1.4 comparisons on average (with chaining, in a table of 125,000 slots, a load factor of 0.8), with no sorting at all. It manages that by a strange-sounding trick: it does not search for the account, it calculates where the account must be and walks straight there.
By the end of this chapter you'll be able to build a hash function by the four classical methods and say why the table size is usually a prime. You'll insert keys into a table by separate chaining, by linear probing, by quadratic probing and by double hashing, and write each key's probe list in an exam answer. You'll delete from a probing table without breaking it, work out the load factor and the expected number of probes, and say when and how a table is rehashed. You'll explain perfect hashing and why a compiler's symbol table is a hash table, choose between a hash table, a balanced tree and a heap for a given job, tell hashing from encryption, and build an LRU cache.