Chapter 7Blazing Fast Lookup with Hash Tables

Imagine that you’re writing a program that allows customers to order food to go from a fast-food restaurant, and you’re implementing a menu of foods with their respective prices. You could, of course, use an array:

 menu = [ [​"french fries"​, 0.75], [​"hamburger"​, 2.5],
 [​"hot dog"​, 1.5], [​"soda"​, 0.6] ]

This array contains several subarrays, and each subarray contains two elements. The first element is a string representing the food on the menu, and the second element represents the price of that food.

As we learned in Chapter 2, Why Algorithms Matter, if this array were unordered, searching for the price of a given food would take O(N) steps since the computer would have to perform a linear ...

Get A Common-Sense Guide to Data Structures and Algorithms now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.