Arrays

Adjacent cells · Part of Lists, stacks and hash

Key idea

Practice

7 questions · about 4 min

Explained step by step

Array

An array stores elements of the same type in adjacent memory spots. Each one's position is called its index, and it starts at 0. The reason is the calculation to find it: element address = first's address + index × size of each element. The index is how many spots you shift from the start, and the first is 0 spots away. It's like building floors in many countries: the ground floor is 0 and the first floor is 1 up.

Since each element's address is computed with a calculation, an array can read any position in O(1): a single step, whether it has 10 or 10 million elements. That's called random access, because you can go anywhere without passing the ones before. It's like a street with numbered houses of equal width: you know house 300 is 300 houses from the corner without ringing a single bell.

In an array there's no free room between elements. To insert one in the middle, you have to shift every later element one spot to the right, and only then write the new one. To delete, you shift every later element to the left. On average half of them move: O(n). And since the size is fixed, inserting loses the last one. It's like squeezing someone into the middle of a row of taken seats: everyone to the right moves over 1.

An array has a fixed size, because the program can't be sure the memory right after it is free: another program or piece of data may be there. To make it bigger, you have to ask for a new, bigger array and copy every element, one by one: O(n). With huge arrays, that copy is slow. It's like moving because the house got too small: you can't add a room if the neighbor is right next door; you have to go to another house and take everything.

Arrays have 3 advantages. They're compact: they store only the data, with no extra references. They have random access in O(1). And they make good use of the cache: when the processor reads an element, it also brings the ones next to it, so walking the array is very fast. Their drawbacks are O(n) insertion and deletion, the fixed size and the leftover space if you reserve too much.

Arrays are everywhere. They're used to pick elements at random, because any position is read in O(1). They're the base for sorting and searching: quicksort, merge sort and binary search work on arrays. They work as lookup tables: if the position is the data, like a letter's ASCII code, the answer is 1 step away. Neural networks use matrices, which are arrays. And stacks, queues, hash tables, heaps and graphs are built with arrays.

Linear search

To find a value in an unsorted array, there's no choice but to look at the elements one by one until you find it. That's called linear search and it costs O(n): in the worst case you look at all of them. Walking an array, looking at each element once, is also O(n). It's like looking for a familiar face in a line of people: you go person by person.

Terms in this lesson

Arki, the beaver mascot

Your next interview starts with 5 minutes today.

Free on iOS and Android. Start from zero, no account needed.

Key idea

Adjacent cells