Static arrays
easy 15 minA static array is a fixed-size block of same-type slots stored side by side in memory.
Video lesson · Chapter 1 of 5
Five fixed sample slots
Static arrays — Juno’s synth rack
1:29Reel
Read caption
Juno calculates a synth memory address in one step, then shifts three presets to make room on the same rack. Index times width, plus the base.
How it works
Step through the idea, or press Play.
Reserve one block. n slots of the same size are laid out side by side, and the size is fixed from then on
Understand it
Start here
What a static array is
A static array is a fixed-size, contiguous block of memory that stores elements of the same type. You choose the size when you create it, and it never grows or shrinks. It is the most basic data structure there is: nearly every other structure, from stacks to hash tables, is built on top of one.
Fixed size
Decided once, at creation. To get more room you must make a new array and copy.
Contiguous
Slots sit next to each other in memory with no gaps and no pointers between them.
Same type, same size
Every slot takes the same number of bytes , which is what makes the address maths work.
O(1) by index
Any index is one calculation away: reading
arr[0]andarr[999999]cost the same.
How much memory it takes
The picture
Memory layout
Take five 4-byte integers whose block starts at address 1000. Each slot begins exactly 4 bytes after the previous one, so the addresses climb in a perfectly regular step.
| Index | Value | Address | Worked out |
|---|---|---|---|
| 0 | 10 | 1000 | |
| 1 | 20 | 1004 | |
| 2 | 30 | 1008 | |
| 3 | 40 | 1012 | |
| 4 | 50 | 1016 | |
| 5 | ? | 1020 | past the end: out of bounds |
The math
Address arithmetic: why access is O(1)
The address of index i
Worked example
Reading arr[i]
arr[i] = x follows the same path and ends with one memory write instead.Two dimensions
Grids and matrices in one block
A 2D array, such as a matrix, an image or a game board, is still one flat block. In row-major order (C, Python, NumPy by default) row 0 is stored first, then row 1, and so on. With columns, cell sits after full rows plus cells.
Row-major address
[1, 0, 0, 1] or a 3D point (x, y, z) are tiny fixed arrays of the same kind.A 3 × 4 grid and its flat indexes
| col 0 | col 1 | col 2 | col 3 | |
|---|---|---|---|---|
| row 0 | 0 | 1 | 2 | 3 |
| row 1 | 4 | 5 | 6 | 7 |
| row 2 | 8 | 9 | 10 | 11 |
The API
Operations and what they cost
| Operation | Best | Worst | Extra space | Why |
|---|---|---|---|---|
Access arr[i] | Address arithmetic | |||
Update arr[i] = x | Address arithmetic, then one write | |||
| Insert at end | Only if a spare slot was reserved | |||
| Insert at index | at the end | at the front | Shifts elements right | |
| Delete at index | at the end | at the front | Shifts elements left | |
| Linear search | first slot | last or missing | Checks slots one by one | |
| Binary search (sorted) | Halves the range each check | |||
| Traverse (sum, max) | Must see every element |
Under the hood
Inserting at a position
insert_at(i, x)
Shifting cost of an insert
def insert_at(data, length, i, value): if length == len(data): raise OverflowError("array is full") for j in range(length, i, -1): # back to front data[j] = data[j - 1] data[i] = value return length + 1data = [10, 20, 40, 50, None] # capacity 5, length 4length = insert_at(data, 4, 2, 30)print(data, length) # [10, 20, 30, 40, 50] 5Under the hood
Deleting at a position
delete_at(i)
Shifting cost of a delete
Searching
Finding a value
Linear search
Expected comparisons when the target is present
| n | Linear search, worst | Binary search, worst |
|---|---|---|
| 5 | 5 | 3 |
| 1,000 | 1,000 | 10 |
| 1,000,000 | 1,000,000 | 20 |
Comparison
Static array vs dynamic array vs linked list
| Operation | Static array | Dynamic array | Linked list |
|---|---|---|---|
| Access by index | |||
| Search by value | |||
| Insert at front | |||
| Insert at end | if room | amortised | with a tail |
| Insert in middle | once you hold the node | ||
| Delete at front | |||
| Grows? | No | Yes, by copying | Yes, node by node |
| Memory per element | Just the value | Value plus spare capacity | Value plus pointer(s) |
| Cache behaviour | Excellent | Excellent | Poor |
Advantages
- access and update by index.
- Cache-friendly, because the memory is contiguous.
- No memory overhead per element.
- Predictable memory use, known up front.
Disadvantages
- Fixed size: it cannot grow or shrink.
- Insert and delete in the middle need shifting.
- Wastes memory if you reserve more than you use.
- The size must be known when you create it.
- 1Allocatea new, bigger block
- 2Copyall n elements across, O(n)
- 3Switchpoint at the new block
- 4Freerelease the old block
A dynamic array automates exactly this, and by growing by a factor it keeps appends $O(1)$ on average.
Hardware
Why arrays are fast in practice
Big O says a linked-list scan and an array scan are both . On real hardware the array usually wins by a wide margin, because of how CPUs fetch memory.
Cache lines
Memory is loaded in chunks of about 64 bytes. One load brings 16 neighbouring 4-byte ints with it.
Prefetching
The CPU spots a steady forward scan and loads the next chunk before you ask for it.
No pointer chasing
A linked list must read a pointer before it knows where the next node is. An array already knows.
import arraya = array.array("i", [10, 20, 30, 40, 50]) # C ints, one blockbase, n = a.buffer_info() # start address, lengths = a.itemsize # bytes per element (usually 4)for i in range(n): print(i, a[i], "at base +", i * s)print("total bytes:", n * s)Pattern spotting
When to reach for an array
Should I use a static array?
Lookup tables
Precomputed values indexed by key: trig tables, Fibonacci numbers, character counts for 26 letters.
Buffers
Fixed-size I/O, audio and video buffers that are filled, flushed and reused.
Matrices and grids
2D arrays for images, game boards and scientific computing.
Caching and memoisation
Store each result at its index so the next lookup is .
Classic array problems to practise next
| Problem | Pattern |
|---|---|
| Two Sum | Hash map of complements |
| Best Time to Buy and Sell Stock | One pass, track the minimum so far |
| Contains Duplicate | Hash set, or sort then compare neighbours |
| Product of Array Except Self | Prefix and suffix products |
| Maximum Subarray | Kadane's running sum |
Recap
Key takeaways
Key points
- A static array is a fixed-size, contiguous block of same-type elements.
- , so access and update are at any index.
- Valid indexes are to ; anything else is out of bounds.
- Insert at shifts elements and delete shifts : both , in the worst case.
- Search by value is , or with binary search if the array is sorted.
- Contiguous memory makes scans cache-friendly; the fixed size is the price.