Sub topic 1
What an array is
Prerequisites
This chapter assumes you can write loops and functions in at least one language. It also assumes you know introductory Big-O notation, such as what O(1) and O(n) mean. You should have a rough idea that memory is a long row of numbered locations called addresses.
The idea
An array is a collection of elements stored one after another in a single block of memory. Each element has an index, which is its position in the sequence. Most languages start counting at 0, so an array of five elements has indexes 0 to 4.
Two properties define a classic array:
- All elements usually have the same type, and so the same size in bytes.
- The block has a fixed length chosen when the array is created.
Because the elements sit side by side, the position of any element follows from simple arithmetic. That is the reason arrays are fast to read, and the next chapters build on it.
What you can do with an array
The basic operations are reading an element by index, writing an element by index, and visiting every element in order. The first two take constant time, written O(1), because the cost does not grow with the array's length. Visiting every element takes O(n), where n is the number of elements.
Python has no built-in fixed-size array type for general values, but a list used with a fixed length shows the operations well:
scores = [10, 20, 30, 40, 50]
print(scores[2]) # read by index: 30
scores[2] = 35 # write by index
print(scores[2]) # 35
total = 0
for value in scores: # visit every element: O(n)
total += value
print(total) # 10 + 20 + 35 + 40 + 50 = 155What an array does not do well
Two limits shape the rest of this tutorial.
- Fixed size. In a true array, the length cannot change after creation. To hold more elements, you must create a larger array and copy the old contents into it.
- Middle changes. Inserting or deleting at the front or in the middle forces you to shift later elements to keep the block gap-free. That costs O(n) in the worst case.
Summary of costs
| Operation | Cost | Reason |
|---|---|---|
| Read by index | O(1) | Position is computed directly |
| Write by index | O(1) | Same computation |
| Traverse all elements | O(n) | Each element is visited once |
| Insert or delete in the middle | O(n) | Later elements must shift |
Check your understanding
Exercise
An array holds 1,000 elements. You read the element at index 3, and then at index 900. Does the second read take longer than the first? Why or why not?
Show answer
No. Both reads take O(1) time. The array's elements are stored in one contiguous block, so the program computes each position directly from the index and does not walk past the earlier elements.
Exercise
Name the two main limits of a classic fixed-size array described in this chapter.
Show answer
First, its length is fixed at creation, so growing it means allocating a larger array and copying. Second, inserting or deleting in the middle requires shifting elements, which costs O(n) in the worst case.