Essential Data Structures home
Array
A row of values stored side by side in memory, each one reached by its position. The simplest structure there is, and the one almost everything else is built on.
What it is
An array stores a fixed number of items of the same kind in one continuous block of memory. Each item has an index, a number that says how far it sits from the start. The address of item i is the start address plus i times the size of one item, so reaching any item takes the same short step, no matter where it sits.
That is the whole trick, and it is why arrays are fast at reading. There is no chain to follow and no search to run. The position itself is the address.
Most languages add a friendlier layer on top. A Python list, a Java ArrayList, and a JavaScript array are dynamic arrays: they look like they can grow without limit, but underneath they are a plain array that is replaced by a bigger one when it fills up. The idea stays the same, the size limit is just hidden.
How it works
Reading or writing by index is a single calculation, so it is constant time. Searching for a value, when you do not know its index, means checking items one by one until you find it.
Inserting or deleting in the middle is the expensive part. Everything after the gap has to shift by one place to make room, or to close the gap. Appending at the end is cheap in a dynamic array most of the time: when the backing array is full, a new one about twice as large is allocated and everything is copied over, which is expensive once, then cheap again for many appends. That average cost is called amortized constant time.
Complexity
| Operation | Average | Worst case |
|---|---|---|
| Access by index | O(1) | O(1) |
| Search by value (unsorted) | O(n) | O(n) |
| Search by value (sorted) | O(log n) | O(log n) |
| Insert or delete at the end | O(1) amortized | O(n) |
| Insert or delete in the middle | O(n) | O(n) |
n is the number of items. A sorted array can be searched with binary search, which halves the remaining range at each step.
When to use it, and when not to
Use an array when you know the size in advance or it changes rarely, when you need to reach items by position, and when you will mostly read or scan in order. Images, audio buffers, tables of numbers, and the backing store of stacks and queues are all arrays underneath.
Do not use a plain array when items are inserted and removed in the middle all the time, or when the size is unknown and keeps changing. That shifting cost adds up. A linked list, a hash table, or a balanced tree fits those shapes better, and each of them is usually built on top of arrays anyway.
Where you meet it
Everywhere. A string is an array of characters. A spreadsheet column, a pixel row in an image, a list of search results, and the arguments passed to a program are all arrays. When a program feels instant at reading the tenth item or the ten-thousandth, that is usually an array doing the work.
In Python
scores = [12, 7, 19, 3, 25]
print(scores[2]) # 19, direct access by index
scores.append(30) # add at the end, cheap on average
scores[1] = 8 # overwrite by index, O(1)
print(scores.index(3)) # search by value, O(n)
In short: an array trades flexibility for speed. Fixed positions give instant access, and the price is paid when something has to move.
Sources
- Array data structure, Wikipedia
- Python tutorial, Data Structures (lists)
- Cormen, Leiserson, Rivest, and Stein, Introduction to Algorithms, chapter 10