Definition · The word-RAM cost modelMemory is an array of words. Reading or writing a word at a known address costs 1, arithmetic on a word costs 1, and nothing else is charged for. Under this model an array index A[i] is one operation, because the address is computed from i; reaching the i-th node of a linked list is i + 1 operations, because the only way to the i-th node is through the ones before it.
This is the model every cost on this course is stated in. It has no cache, no page, and no notion of one access being nearer than another — System Design’s storage course is where that assumption is dropped and the answers change.
A dynamic array stores the elements contiguously, with spare capacity at the end. A doubly-linked list stores each element in its own node with pointers to its neighbours, and keeps a pointer to the head and to the tail. Both implement the same abstract sequence. Here is what each charges:
index i search insert front insert end delete at i
dynamic array 1 n n + 1 1 n − i
doubly-linked list i + 1 n 1 1 i + 2
the array pays to MAKE ROOM the list pays to ARRIVE
Read the two rows against each other rather than down. insert front is n + 1 on the array because every one of the n elements shifts up one place and then the new element is written; it is 1 on the list because two pointers change. index i is 1 on the array because the address is arithmetic; it is i + 1 on the list because the walk is the only route. delete at i costs the array the n − i elements it must shift down, and costs the list i + 2: i + 1 to arrive and one splice.
Example · One insertion at the front, both waysWith n = 8 elements, inserting at the front costs the array 9 and the list 1. The lab reports exactly that pair, and it is the single most quoted comparison in this subject. It is also the comparison that produces the mistake, because the next operation in almost every real sequence is a lookup, and the lookup reverses it.
The sequence, executed
The lab’s opening sequence is the four operations the model is stated on — index, insert at the front, insert at the end, delete from the middle — repeated four times from n = 16, reaching halfway along for the two that take a position. Sixteen operations. Grouped by kind, the counters read:
operation how many array list
index 4 4 38
insert at the front 4 74 4
insert at the end 4 4 4
delete from the middle 4 40 46
total 16 122 92
The list wins this sequence, 92 against 122, and the operation that decided it is the insertion at the front: four of them cost 74 on the array and 4 on the list, a gap of 70 in a contest settled by 30. Every other row is either close or the wrong way round. That is the useful way to read a cost comparison — find the row with the largest gap, because that row is the answer and the rest is noise.
Example · Two sequences that reverse the verdictChange nothing but the mix. Three insertions at the front and one index, four times over from n = 24: array 370, list 78. Three indexes and one insertion at the end, same size: array 16, list 250. Same two representations, same counters, same model — a factor of nearly five one way and a factor of fifteen the other. The representation does not win or lose; a workload decides.
Where the counted number and the proved bound part company
One entry in that table is not a worst case. insert end on a dynamic array is written 1, and sometimes it is not 1: when the spare capacity runs out the array is reallocated and every element is copied. The 1 is the amortised cost — the average over a run of insertions, which is O(1) because doubling makes the copies rare. The lab charges 1 every time and never shows the copy, so the array column is a little optimistic about any single insertion at the end, and honest about a run of them.
That gap is worth naming on the first page of the course, because it is the shape of every disagreement to come. Three different promises hide behind a single small number: a worst case that no input beats, an amortised cost that holds on average over a sequence, and an expected cost that holds on average over some randomness. A measurement cannot tell them apart. 122 and 92 are two integers produced by running sixteen operations; they are not a bound, and the next lesson is about a structure whose whole interest is that the distinction matters.