Mental model
Imagine a numbered row of boxes. Because every box has the same size, the computer can calculate the address of box i without visiting the boxes before it.
Everyday analogy
A row of cinema seats: the seat number takes you directly to the right position.
Learn by doing
Interactive visualization
Try multiple operations. Read the trace after every action and connect the visible movement to the operation's complexity.
Interactive data structure
Change the structure and watch the operation
Try an operation and connect the visible movement to its runtime cost.
Reasoning checkpoint
Pause and explain the trace
Try to answer each prompt before revealing the explanation.
01RepresentationHow does the physical representation explain the operations you saw?
Imagine a numbered row of boxes. Because every box has the same size, the computer can calculate the address of box i without visiting the boxes before it.
02CostWhy can “Search” become O(n)?
An unsorted array may require checking every value.
03CorrectnessWhich implementation mistake should you test explicitly?
Reading or writing outside valid indexes
Cost model
Operation complexity
| Operation | Average | Worst | Why |
|---|---|---|---|
| Access by index | O(1) | O(1) | The address is calculated directly. |
| Search | O(n) | O(n) | An unsorted array may require checking every value. |
| Insert at end | O(1)* | O(n) | Dynamic arrays occasionally resize and copy. |
| Insert or delete in middle | O(n) | O(n) | Following values must shift. |
* Amortized: occasional expensive work is spread across many cheap operations.
From picture to program
Java implementation
int[] scores = {12, 25, 31, 47};
// Direct access: O(1)
int second = scores[1];
// Linear search: O(n)
for (int i = 0; i < scores.length; i++) {
if (scores[i] == 31) {
System.out.println("Found at " + i);
}
}Where it appears
- Ordered collections and lookup tables
- Matrices, grids, and dynamic-programming tables
- Building blocks for heaps, hash tables, stacks, and queues
Common mistakes
- Reading or writing outside valid indexes
- Forgetting the shift cost of middle insertions
- Assuming a dynamic array never reallocates