All pages 1 of 8

The array

A row of numbered drawers: you can open any one at once, but a new thing near the front makes the rest move up.

01234567
In life: Every drawer has a number, so you can open any one at once.
01234567478151623
Found 16 in 5 steps.

Counted: 5 steps

Work the machine

The steps

  1. Drawer 0 holds 4, not 16, so open the next one.
  2. Drawer 1 holds 7, not 16, so open the next one.
  3. Drawer 2 holds 8, not 16, so open the next one.
  4. Drawer 3 holds 15, not 16, so open the next one.
  5. Drawer 4 holds 16: that is it.
  6. Found 16 in 5 steps.
The machine, at the end of the worked example. Press a button to make a move and watch it, one step at a time.
find: every one8 stepsadd: every one9 stepsremove: every one8 steps
The costs, with 8 things inside, counted by running the machine above: one box a step.

What it is

Think of a chest of eight drawers with a number on each. The first drawer is number 0, the next is 1, and so on up to 7. Each drawer holds one thing.

In a computer, this is called an array. The things sit side by side in memory, in order, with no gaps. The number on a drawer is called its index.

How a move works

To read drawer 3, you go straight to it. You do not open drawers 0, 1 and 2 first. It takes 1 step, however many drawers there are.

Finding a number is slower. If you do not know where 16 is, you open the drawers one by one from the front. In 4, 7, 8, 15, 16, 23, you meet 16 in the fifth drawer, so it takes 5 steps.

Putting a new thing in the middle is the slowest. Say the drawers hold 4, 8, 15, 16 and 23, and 7 must go in drawer 1. First 23 moves up one drawer, then 16, then 15, then 8. Then 7 is written in. That is 5 steps.

Where you meet it

Arrays are everywhere. A list of high scores, the dots of colour in a photo and the days of a month are all kept in arrays. Most of the other things in this series are built on top of one.

Watch out for

One thing in at the front moves everything behind it. With eight things inside, that is 9 steps: eight moves and a write. Taking the front one out is just as bad. If you add and take at the front a lot, the next page may suit you better.