📚 IB Edexcel Computer Science: Arrays – Key Concepts and Exam Focus | IB Edexcel 计算机科学:数组 考点精讲
Arrays form the backbone of data structures in both IB and Edexcel computer science curricula. Mastering how arrays are declared, manipulated in pseudocode, and mapped to memory is essential for achieving high marks on algorithms, data representation, and problem-solving questions. This revision guide walks you through every critical concept, from basic indexing to dynamic memory allocation, with bilingual explanations and practical pseudocode examples.
数组是 IB 与 Edexcel 计算机科学课程中最核心的数据结构之一。熟练掌按数组的声明方式、在伪代码中的操作方法以及内存映射关系,是在算法题、数据表示和问题求解中拿高分的关键。本精讲带你逐一攻克所有核心考点,从基础索引到动态内存分配,配有双语解释和实用的伪代码示例。
1. What Is an Array? | 数组的定义与特性
An array is a homogeneous, contiguous block of memory that stores a fixed number of elements of the same data type. Each element can be accessed directly via an integer index. The ability to randomly access any element in O(1) time makes arrays indispensable for implementing algorithms like binary search, sorting, and hash tables.
数组是一块连续的同质内存区域,用于存储固定数量、相同数据类型的元素。可通过整数索引直接随机访问任一元素,该操作时间复杂度为 O(1)。这一特性使数组在实现二分搜索、排序和哈希表等算法时不可或缺。
- Elements must be of the same type (e.g. all INTEGER, all STRING).
- 元素必须为同一类型(例如全部为 INTEGER 或全部为 STRING)。
- Size is usually declared at creation and cannot be changed in static arrays.
- 静态数组在创建时声明大小,且不可更改。
- Memory addresses are calculated as: Base_Address + Index × Element_Size.
- 内存地址计算公式为:基地址 + 索引 × 元素大小。
Address(A[i]) = Base(A) + i × k
2. One-Dimensional Arrays | 一维数组
A one-dimensional array is the simplest form: a linear list of elements. In Edexcel pseudocode, we declare it with a type keyword or by specifying a size. For example: DECLARE scores : ARRAY[1..30] OF INTEGER creates an array of 30 integers indexed from 1 to 30. Alternatively, 0‑based indexing is used when the lower bound is 0.
一维数组是最简单的线性元素列表。在 Edexcel 伪代码中,通过类型关键字或指定大小声明。例如 DECLARE scores : ARRAY[1..30] OF INTEGER 创建一个含 30 个整数的数组,下标从 1 至 30。若下界为 0,即为基于 0 的索引。
Key operations:
关键操作:
- Initialisation:
scores ← [0,0,0...]or a loop. - 初始化:
scores ← [0,0,0...]或通过循环赋值。 - Reading/writing:
OUTPUT scores[5],scores[3] ← 98. - 读写:
OUTPUT scores[5]、scores[3] ← 98。
3. Two-Dimensional Arrays | 二维数组
A 2D array is an array of arrays, often visualised as a table with rows and columns. Declaration: DECLARE grid : ARRAY[0..2, 0..3] OF REAL creates a 3×4 matrix. Access follows the convention grid[row, col]. Two‑dimensional arrays are frequently tested in matrix operations, board games, and image processing algorithms.
二维数组即数组的数组,通常可视化为带有行和列的表格。声明方式为 DECLARE grid : ARRAY[0..2, 0..3] OF REAL,创建一个 3×4 矩阵。访问格式为 grid[row, col]。二维数组常出现在矩阵运算、棋盘游戏和图像处理算法的考题中。
When traversing a 2D array, nested FOR loops are used:
遍历二维数组时使用嵌套的 FOR 循环:
FOR i ← 0 TO 2
FOR j ← 0 TO 3
grid[i,j] ← 0.0
NEXT j
NEXT i
4. Indexing and Bounds Checking | 索引与越界检查
Index values must lie within the declared bounds. Accessing arr[-1] or arr[101] when the array size is 100 leads to an index out of bounds error. In high‑level languages, this usually triggers a runtime error; in pseudocode exams, you are expected to explicitly check bounds using IF statements before accessing.
索引值必须落在声明范围内。当数组大小为 100 时,访问 arr[-1] 或 arr[101] 会导致索引越界错误。在高级语言中通常触发运行时错误;在伪代码考试中,你应在访问前用 IF 语句显式进行边界检查。
IF index >= lower AND index <= upper THEN
0‑based indexing: lower = 0; 1‑based: lower = 1. Always clarify the convention before writing algorithms.
基于 0 的索引:下界 = 0;基于 1 的索引:下界 = 1。在编写算法前务必明确所用约定。
5. Traversal and Aggregate Operations | 遍历与汇总操作
Traversal means visiting every array element exactly once, usually with a FOR loop. Common aggregate operations include summing, finding the maximum/minimum, counting, and averaging. These are fundamental building blocks for more complex algorithms.
遍历指通过 FOR 循环恰好访问每个数组元素一次。常见的汇总操作包括求和、寻找最大值 / 最小值、计数和求平均值。这些都是更复杂算法的基本构件。
- Loop counter serves as the index:
FOR i ← 0 TO length-1. - 循环计数器充当下标:
FOR i ← 0 TO length-1。 - Linear search is a traversal that stops early when a match is found.
- 线性搜索是一种遍历,找到匹配项后提前终止。
Example: summing all elements
示例:求所有元素之和
sum ← 0
FOR i ← 1 TO LENGTH(arr)
sum ← sum + arr[i]
NEXT i
6. Insertion and Deletion in Static Arrays | 静态数组中的插入与删除
Because static arrays have a fixed capacity, insertion and deletion are simulated by shifting elements. For insertion, all elements from the desired position to the last occupied index must be moved one place to the right. For deletion, elements are shifted left to compact the array. A separate variable often tracks the current number of valid elements.
由于静态数组容量固定,插入和删除是通过元素移位来模拟的。插入时,从目标位置到最后一个有效索引的所有元素都需向右移动一位。删除时,元素向左移动以保持紧凑。常用一个单独变量记录当前有效元素个数。
Insert: FOR i ← last DOWNTO pos
arr[i+1] ← arr[i]
Insert:FOR i ← last DOWNTO pos
arr[i+1] ← arr[i]
Time complexity: O(n) for both insertion and deletion due to shifting.
由于移位,插入与删除的时间复杂度均为 O(n)。
7. Searching Algorithms on Arrays | 基于数组的搜索算法
Two core search algorithms are examined: linear search and binary search. Linear search works on unsorted arrays, comparing each element with the target until found or the end is reached. It has O(n) complexity. Binary search requires a sorted array and repeatedly divides the search interval in half, achieving O(log n).
考试涉及两种核心搜索算法:线性搜索与二分搜索。线性搜索适用于无序数组,逐个比较目标值直至找到或遍历结束,复杂度为 O(n)。二分搜索要求数组有序,通过不断将搜索区间减半实现 O(log n) 的效率。
- Binary search precondition: array must be sorted.
- 二分搜索的前置条件:数组必须已排序。
- Use left and right pointers, and a mid calculation.
- 使用左指针、右指针和中间下标的计算。
Binary search pseudocode skeleton:
二分搜索伪代码框架:
left ← 0
right ← LENGTH(arr)-1
WHILE left ≤ right
mid ← (left + right) DIV 2
IF arr[mid] = target THEN
RETURN mid
ELSE IF arr[mid] < target THEN
left ← mid + 1
ELSE
right ← mid - 1
ENDIF
ENDWHILE
RETURN -1
8. Sorting Algorithms with Arrays | 数组排序算法
Bubble sort and insertion sort are commonly assessed. Bubble sort repeatedly swaps adjacent elements if they are in the wrong order, moving the largest unsorted element to its correct position each pass. In pseudocode, a nested loop with a flag to detect early termination is often required.
冒泡排序与插入排序是常考算法。冒泡排序通过反复交换顺序错误的相邻元素,每轮将当前未排序部分的最大元素冒泡至正确位置。伪代码通常要求使用嵌套循环,并设置标志位实现提前终止。
Bubble sort with optimisation:
优化版冒泡排序:
FOR i ← 0 TO n-2
swapped ← FALSE
FOR j ← 0 TO n-2-i
IF arr[j] > arr[j+1] THEN
SWAP arr[j], arr[j+1]
swapped ← TRUE
ENDIF
NEXT j
IF NOT swapped THEN BREAK
NEXT i
Insertion sort builds the sorted array one element at a time, suitable for small or nearly sorted data sets.
插入排序每次将一个元素插入已排序部分,适用于小规模或基本有序的数据集。
9. Dynamic Arrays and Memory Allocation | 动态数组与内存分配
Some exam questions ask to compare static and dynamic arrays. Static arrays have a fixed size determined at compile time, whereas dynamic arrays can grow or shrink at runtime using heap memory. In pseudocode you may see ARRAY without a fixed bound, or explicit operations like RESIZE.
部分考题要求学生比较静态与动态数组。静态数组大小在编译时固定,而动态数组可利用堆内存在运行时扩容或缩容。伪代码中可能出现无固定边界的 ARRAY,或显式操作如 RESIZE。
- Static: faster, safer, but inflexible.
- 静态:更快、更安全,但灵活性差。
- Dynamic: flexible, but may cause memory fragmentation and require explicit memory management.
- 动态:灵活,但可能导致内存碎片并需要显式管理。
Amortised analysis shows that dynamic array resizing (doubling strategy) still gives O(1) average insertion time.
摊还分析表明,动态数组的倍增策略仍然拥有 O(1) 的平均插入时间。
10. Array Representation in Memory | 数组的内存表示
Understanding the low‑level memory layout helps with pointer arithmetic and cache efficiency. An array of n elements of type T occupies n × sizeof(T) consecutive bytes. The address of A[i] is given by Base + i * size. For multi‑dimensional arrays, storage order matters: row‑major order stores rows consecutively, while column‑major order stores columns consecutively.
理解底层内存布局有助于掌握指针运算和缓存效率。一个包含 n 个元素、类型为 T 的数组占据 n × sizeof(T) 个连续字节。A[i] 的地址 = Base + i * size。对于多维数组,存储顺序非常关键:行优先将行连续存储,列优先则将列连续存储。
Row‑major: A[i,j] = Base + (i × cols + j) × element_size
Edexcel pseudocode follows row‑major order, similar to C and Python.
Edexcel 伪代码遵循行优先的顺序,与 C 和 Python 一致。
11. Common Exam Pitfalls and Tips | 常见考试陷阱与应对技巧
Many marks are lost due to small mistakes: off‑by‑one errors in loop bounds, confusing 0‑based and 1‑based indexing, forgetting to initialise arrays, and not handling empty arrays. Always dry‑run your pseudocode with a tiny example (e.g. array of 3 elements) to verify correctness.
许多失分源于小错误:循环边界差 1 的错误、混淆基于 0 与基于 1 的索引、忘记初始化数组以及未处理空数组。务必用微型示例(如长度为 3 的数组)手动跟踪伪代码以验证正确性。
- Use meaningful variable names like
total,maxValue,found. - 使用有意义的变量名,如
total、maxValue、found。 - Always state preconditions (e.g. array is sorted) before a binary search.
- 在二分搜索前一定说明前置条件(如数组已排序)。
- When swapping, don't lose a value: use a temporary variable or
SWAPkeyword. - 交换时避免丢失值:使用临时变量或
SWAP关键字。
12. Summary and Revision Checklist | 总结与复习清单
Arrays are a high‑yield topic that connects data structures, algorithms, and memory management. Ensure you can: declare and initialise 1D and 2D arrays; traverse with loops; implement linear and binary search; code bubble and insertion sorts; simulate insertions and deletions with shifting; explain static vs dynamic; and compute memory addresses in row‑major order.
数组是连接数据结构、算法与内存管理的高分值主题。复习时要确保能够:声明并初始化一维和二维数组;使用循环遍历;实现线性搜索与二分搜索;编写冒泡和插入排序;通过移位模拟插入和删除;解释静态与动态数组的区别;并计算行优先顺序下的内存地址。
| Topic | 主题 | Self‑Check |
| 1D array operations | 一维数组操作 | ☐ |
| 2D array traversal | 二维数组遍历 | ☐ |
| Linear & binary search | 线性搜索与二分搜索 | ☐ |
| Bubble & insertion sort | 冒泡排序与插入排序 | ☐ |
| Insert/delete with shift | 移位插入 / 删除 | ☐ |
| Static vs dynamic arrays | 静态与动态数组 | ☐ |
| Memory address calculation | 内存地址计算 | ☐ |
Published by TutorHao | Computer Science Revision Series | aleveler.com
更多咨询请联系16621398022(同微信)
屏轩国际教育cambridge primary/secondary checkpoint, cat4, ukiset,ukcat,igcse,alevel,PAT,STEP,MAT, ibdp,ap,ssat,sat,sat2课程辅导,国外大学本科硕士研究生博士课程论文辅导