Array
Picture an egg carton: a row of little cups, each numbered, each the same size. An array is exactly that for your data — a straight row of numbered slots, side by side. Because every slot is identical and the row starts at a known spot, the computer can leap straight to slot number 5 without checking slots 0 through 4 first. That one trick is what makes arrays the workhorse behind almost everything else you will learn.
What is it
An array is a row of numbered boxes sitting side by side in memory. Each box holds one value, and every box is the same size. The numbering starts at 0, so the first box is box 0, the second is box 1, and so on. Because the boxes are identical and touch each other, the computer can find any box by simple arithmetic instead of searching.
How it works
The array remembers just one thing: where box 0 lives. To reach box 5, it takes that starting spot and steps forward 5 box-widths. Same-size boxes mean each step is the same distance, so the jump is one quick calculation. No matter how long the row is, finding a box by its number is a single move. This is why the boxes must touch with no gaps — the arithmetic only works if the row is unbroken.
Operations
| Name | Big-O | Why |
|---|---|---|
| Access by index | O(1) | jump straight to the box by its number, one calculation |
| Update a value | O(1) | find the box instantly, then overwrite what's inside |
| Search (unsorted) | O(n) | the value could be in any box, so you may check them all |
| Insert at the end | O(1) | drop it in the next free box, nothing else moves |
| Insert in the middle | O(n) | every box after it has to shuffle one place over |
| Delete from the middle | O(n) | the gap has to close, so everything after slides back |
When to use
Reach for an array when you read a lot by position — box 3, box 900, box 12 — because each of those is one instant step. Use it when the size barely changes, so you rarely pay for shuffling. It is also the quiet foundation under bigger structures: stacks, queues, and hash tables are all arrays wearing a costume. When in doubt, an array is the honest first choice.
Watch out for
The catch is shifting. Insert or delete anywhere but the end, and every box after the change has to move over to keep the row unbroken — slow work on a long row. Classic fixed-size arrays have a second catch: you pick the length up front, and full means full. Growable versions hide this by quietly copying everything into a bigger row when they run out, which is fast on average but occasionally a big move all at once.
Analogy
An egg carton. Numbered cups in a straight row, all the same size. You can grab the egg in cup 7 instantly — but slide a new egg into the middle and every egg after it has to budge over to make room.
Practice lessons
Bubble Sort
Imagine kids lining up by height, but each kid can only compare with the neighbour directly beside them and swap places.
Reverse an Array
Think of a line of people who need to face the other way without leaving their spots empty.
Find the Maximum
Imagine judging a tallest-person contest where contestants walk past you one at a time.
Rotate an Array
Picture people seated around the last row of a cinema playing musical chairs to the right.
Palindrome Check
Fold a paper strip with numbers written on it exactly in half.
Two Sum (Sorted)
Two shoppers stand at opposite ends of a price-sorted shelf with a fixed budget for exactly two items.