Sub topic 2
Why arrays are fast and what they lack
This chapter assumes you can write loops and functions, know what a list or array is, and have seen Big-O notation. It explains what makes array access fast and where arrays fall short as key-value stores. A hash table is built to fix that gap.
Why indexing is constant time
An array stores its elements in one unbroken block of memory. Every element has the same size. The computer therefore finds element i with one calculation:
address of item i = base address + i * element sizeSuppose the array starts at address 1000 and each element takes 8 bytes. Item 5 is at 1000 + 5 * 8 = 1040. The computer reads that address directly. It does not look at items 0 through 4 first.
The cost is the same for an array of ten items or ten million. That is O(1), or constant time. Python lists, Java arrays, and C arrays all use this idea. Python lists store references to objects, but the position lookup works the same way.
What arrays lack
The fast path only works when you know the position. Real programs usually know something else, such as a username, a word, or a product code. They want the value that goes with that key.
An array gives you two bad options:
- Search by value. You scan from the start and compare each element. A miss means checking every element, which is O(n).
- Use the key as the index. This only works if the key is a small non-negative integer. It does not work for the string
"apple"or the tuple(3, 4).
names = ["ada", "bob", "cy", "dee"]
# Access by position: one address calculation.
print(names[2])
# Access by value: check elements one at a time.
def find(items, target):
for i, item in enumerate(items):
if item == target:
return i
return -1
print(find(names, "dee"))
print(find(names, "zed"))This prints cy, 3, and -1. The first lookup is O(1). The second must walk the whole list to reach "dee". The third checks every element and then reports a miss.
A sorted array with binary search brings lookup down to O(log n). Inserting into the middle of a sorted array then costs O(n), because later elements must shift.
The idea hash tables build on
Two facts point to the solution:
- Array access by index is O(1).
- We want to look up by arbitrary keys.
If we could turn any key into a valid array index, we could use the fast path for every lookup. The conversion has to be quick and repeatable. Equal keys must always give the same index. The next chapters cover how to do this and what goes wrong when two keys land on the same index.
Common mistakes
- Assuming
x in my_listis fast. It is a linear scan, so it costs O(n). - Treating a dictionary lookup as if it were a list index. A dictionary works because of an extra step that converts the key to a position.
- Forgetting that O(1) access needs the position. The array does not know which position holds a given value.
Exercise
An array holds 1,000,000 names. How much work does it take to read the name at index 999,999, and how much to find out whether the name "zed" is present when it is not stored?
Show answer
Reading index 999,999 takes one address calculation (base + 999,999 * element size), so it is O(1). Checking for "zed" requires comparing against all 1,000,000 names, so it is O(n).