How are arrays and records stored, and why is array access so fast?
Describe arrays and records as data structures, explain constant-time array indexing and contrast static with dynamic arrays
A focused answer to the H2 Computing outcome on arrays and records. Contiguous storage and constant-time indexing, one and two dimensional arrays, records as fields of mixed type, and static versus dynamic arrays.
Reviewed by: AI editorial process; not yet individually human-reviewed
Have a quick question? Jump to the Q&A page
Jump to a section
What this dot point is asking
SEAB wants you to describe arrays and records as data structures, explain why array indexing is constant time, and contrast static with dynamic arrays. The central idea is that an array's contiguous layout lets any element be reached by a single address calculation, while a record bundles related values of different types into one named unit.
The answer
Arrays and contiguous storage
An array is an ordered collection of elements of the same type, stored in contiguous (adjacent) memory locations and accessed by an integer index starting at 0. Because the elements sit back to back, the address of element is:
This single multiplication and addition is independent of the array's length, so reading or writing array[i] is - constant-time random access. This direct addressing is the array's defining strength.
The cost of insertion and deletion
The same fixed layout makes structural changes expensive. Inserting at the front, or deleting from the front, requires shifting every following element, which is . Only appending at the end (with room) is cheap.
Two-dimensional arrays
A 2D array (a grid or matrix) is indexed by row and column, grid[r][c], and is typically stored row by row in memory (row-major order). The address arithmetic extends naturally, so element access is still .
Records
A record (or struct) groups several fields, which may be of different types, under one name:
# a record-like structure for one student
student = {"name": "Wei Ling", "age": 17, "grade": "A"}
Where an array holds many items of one type, a record holds related items of mixed types as one logical unit. An array of records then stores many such units, indexed by position - far safer than keeping several parallel arrays in step.
Static versus dynamic arrays
- A static array has a fixed size, set at creation; it cannot grow.
- A dynamic array can resize: when full, it allocates a larger block and copies the elements, giving amortised appends despite occasional costly copies. Python's
listis a dynamic array.
Examples in context
Example 1. An image as a 2D array. A greyscale image is a grid of pixel values stored as a two-dimensional array. Reading pixel (r, c) is instant address arithmetic, which is why image filters that touch every pixel are efficient - each access is and the whole pass is .
Example 2. A record of fixed format. A weather station logs each reading as a record of timestamp, temperature, humidity and pressure. Storing readings as an array of these records keeps every reading's fields together and lets the program jump to the thousandth reading instantly by index, rather than juggling four separate arrays.
Try this
Q1. State and justify the time complexity of reading array[100] in an array of 1000 elements. [2 marks]
- Cue. - the address is computed directly from base, index and element size, regardless of length.
Q2. Why is inserting an element at the front of an array ? [1 mark]
- Cue. Every existing element must shift one place right to make room, which is moves.
Q3. Give one advantage of an array of records over several parallel arrays. [1 mark]
- Cue. Each item's fields stay bundled together, so the arrays cannot fall out of step and one record moves as a unit.
Exam-style practice questions
Practice questions written in the style of SEAB exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.
Original5 marks(a) Explain why accessing an element of an array by its index is an operation. (b) State the time complexity of inserting a new element at the start of an array of elements, and explain why. (c) Distinguish between a static and a dynamic array.Show worked answer →
(a) An array stores its elements in contiguous memory. The address of element is computed directly as , a single arithmetic step independent of , so indexing is .
(b) Inserting at the start is : every existing element must be shifted one place to the right to make room, which is moves.
(c) A static array has a fixed size set when it is created and cannot grow. A dynamic array can grow (and shrink); when it fills, it allocates a larger block and copies the elements across, giving amortised appends.
Markers reward the address-arithmetic reason for indexing, the shifting that makes front insertion , and the fixed-versus-resizable distinction.
Original4 marksA program stores student data where each student has a name (text), an age (integer) and a grade (single character). (a) Explain why a record is a suitable structure for one student. (b) Explain how an array of records would store many students, and give one advantage over separate parallel arrays.Show worked answer →
(a) A record groups several fields of different types under one name, so a single Student record holds name (string), age (integer) and grade (char) together as one logical unit. This keeps related data of mixed type bundled, rather than scattered.
(b) An array of records stores many students as a sequence of Student records, indexed by position - students[3] is the fourth student, and students[3].age is their age. An advantage over three separate parallel arrays (names[], ages[], grades[]) is that each student's data stays together, so there is no risk of the arrays falling out of step, and adding or moving a student moves one record rather than synchronising several arrays.
Markers reward the record as a named group of mixed-type fields, the array-of-records indexing, and the cohesion/consistency advantage over parallel arrays.
Related dot points
- Describe stacks (LIFO) and queues (FIFO), implement their core operations, and identify suitable applications of each
A focused answer to the H2 Computing outcome on stacks and queues. The LIFO stack and FIFO queue, their push, pop, enqueue and dequeue operations with overflow and underflow, and typical applications of each.
- Describe a linked list as nodes joined by pointers, implement insertion and deletion, and contrast it with an array
A focused answer to the H2 Computing outcome on linked lists. Nodes and pointers, traversal, inserting and deleting nodes by relinking, and the trade-offs against arrays for access and modification.
- Describe a hash table and hash function, explain collision resolution by chaining and open addressing, and analyse the effect of load factor
A focused answer to the H2 Computing outcome on hash tables. Hash functions mapping keys to buckets, average O(1) lookup, collisions resolved by chaining or open addressing, and how the load factor affects performance.
- Implement and trace bubble sort and insertion sort, analyse their complexity, and compare their behaviour on nearly sorted data
A focused answer to the H2 Computing outcome on elementary sorts. Bubble sort and insertion sort, tracing each on an example, their O(n squared) worst case, and why insertion sort excels on nearly sorted input.
- Define classes with attributes and methods, create objects, and apply encapsulation, inheritance and polymorphism in Python
A focused answer to the H2 Computing outcome on object-oriented programming. Classes and objects, attributes and methods, the constructor, and the principles of encapsulation, inheritance and polymorphism in Python.