Sub topic 4
Arrays and Index Conventions
This chapter assumes you have used arrays or lists in at least one language and know how to read an element by its index. It covers how pseudocode names array positions, why the starting index matters, and the errors that come from mixing conventions.
What an Array Means in Pseudocode
An array is a fixed sequence of elements, each reached by an integer index. Pseudocode writes A[i] for the element at index i, and A[1..n] for the whole array of length n. Most algorithm textbooks use 1-based indexing: the first element is A[1] and the last is A[n]. Most programming languages, including Python, C, Java, and JavaScript, use 0-based indexing: the first element is a[0] and the last is a[n - 1].
Neither convention is wrong. The problems start when you leave the convention unstated, or when you switch conventions partway through an algorithm.
Comparing the Two Conventions
| Question | 1-based (pseudocode) | 0-based (most languages) |
|---|---|---|
| First element | `A[1]` | `a[0]` |
| Last element | `A[n]` | `a[n - 1]` |
| Loop over every element | `FOR i ← 1 TO n` | `for i in range(0, n)` |
| Loop skipping the first | `FOR i ← 2 TO n` | `for i in range(1, n)` |
| Middle index (n odd) | `(n + 1) / 2` | `(n - 1) / 2` |
| Invalid index just past the end | `A[n + 1]` | `a[n]` |
The mapping between the two is one subtraction: pseudocode index i corresponds to code index i - 1. The element count n stays the same.
Common Indexing Errors
- Reading position 0 in a 1-based algorithm.
A[0]does not exist. Loops written asFOR i ← 0 TO nread one element too many. - Reading position n in a 0-based program.
a[n]is past the end. In Python this raisesIndexError. In C it silently reads memory you do not own. - Half-converted loops. You change
A[1]toa[0]but keep the loop boundTO n, so the last iteration overruns. - Neighbour access at the boundary. Expressions such as
A[i + 1]orA[i - 1]need the loop range shrunk by one at the matching end. - Inclusive vs exclusive ranges. Pseudocode
FOR i ← 1 TO nincludesn. Python'srange(1, n)excludesn.
The snippet below prints each 1-based pseudocode index next to the 0-based Python index it maps to, so you can check the mapping on a real array:
a = [3, 7, 2, 9]
n = len(a)
for i in range(1, n + 1): # i runs 1..n, as in pseudocode
print(f"A[{i}] -> a[{i - 1}] = {a[i - 1]}")
# A[1] -> a[0] = 3
# A[2] -> a[1] = 7
# A[3] -> a[2] = 2
# A[4] -> a[3] = 9Exercise
A 1-based pseudocode loop compares neighbours with FOR i ← 1 TO n - 1 DO IF A[i] > A[i + 1] THEN .... Give the equivalent Python loop header and comparison for a 0-based list a of length n, and name the largest index each version reads.
Show answer
for i in range(0, n - 1): with if a[i] > a[i + 1]:. The pseudocode's largest index is A[n], when i = n - 1. The Python version's largest is a[n - 1], when i = n - 2. Both read the last element and never go past it.
Exercise
Someone converts FOR i ← 1 TO n DO total ← total + A[i] into Python as for i in range(1, n): total += a[i]. What does this code skip?
Show answer
It skips a[0], the first element, and stops at a[n - 1], so it adds only n - 1 elements. The correct version is for i in range(0, n): total += a[i], or range(n).