IB CIE Computer Science: Arrays – Key Points Revision | IB CIE 计算机:数组 考点精讲

📚 IB CIE Computer Science: Arrays – Key Points Revision | IB CIE 计算机:数组 考点精讲

Arrays are one of the most fundamental data structures in computer science, and they appear in virtually every IB and CIE syllabus topic from algorithm design to data representation. Whether you are preparing for Paper 2 algorithmic problem-solving or tackling Paper 4 high-level programming, a solid grasp of arrays – their indexing, traversal, searching, and sorting – is essential. This revision guide breaks down every key concept you need to master, complete with pseudocode examples and common pitfalls.

数组是计算机科学中最基础的数据结构之一,几乎出现在 IB 和 CIE 教学大纲的每一个专题中,从算法设计到数据表示。无论你是在备考 Paper 2 的算法问题解决,还是应对 Paper 4 的高级编程,牢固掌握数组的索引、遍历、查找和排序都至关重要。本考点精讲将逐一拆解你需要掌握的核心概念,并配有伪代码示例和常见错误解析。

1. Array Definition and Characteristics | 数组的定义与特性

An array is a static, homogeneous data structure that stores a fixed-size sequential collection of elements of the same data type. Each element is accessed by its index, which typically starts at 0 in IB pseudocode and most high-level languages. Arrays are stored in contiguous memory locations, allowing O(1) random access.

数组是一种静态、同质的数据结构,存储固定大小的、相同数据类型的元素顺序集合。每个元素通过其索引访问,在 IB 伪代码和大多数高级语言中索引通常从 0 开始。数组存储在连续的内存位置中,允许 O(1) 的随机访问。

The size of an array is defined at declaration and cannot be changed at runtime (unlike lists in Python). This immutability of size is a common exam topic: attempting to access indices outside the declared range leads to an ‘index out of bounds’ error.

数组的大小在声明时确定,且不能在运行时改变(不同于 Python 中的列表)。这种大小不可变性是常见考点:试图访问超出声明范围的索引会导致“索引越界”错误。


2. Array Indexing and Boundaries | 数组索引与边界

In exam pseudocode, an array A of size n is typically declared with indices 0 to n‑1. The first element is A[0], and the last is A[n‑1]. Understanding this zero-based indexing is crucial for loop conditions, as off-by-one errors are a major source of lost marks.

在考试伪代码中,大小为 n 的数组 A 通常声明索引为 0 到 n‑1。第一个元素是 A[0],最后一个是 A[n‑1]。理解这种从零开始的索引对循环条件至关重要,因为“差一错误”是丢分的主要原因。

Some older CIE past papers might use 1-based indexing for clarity in algorithm descriptions. Always read the question carefully to confirm the index range. In any case, the upper bound is length‑1 in zero-based systems, and the lower bound is 0.

一些较旧的 CIE 真题可能会为了算法描述的清晰性而使用基于 1 的索引。务必仔细审题以确认索引范围。无论如何,在基于零的系统中,上界是 length‑1,下界是 0。


3. Declaring and Initializing 1D Arrays | 一维数组的声明与初始化

In IB and CIE pseudocode, a typical declaration looks like: DECLARE scores : ARRAY[0:9] OF INTEGER. This creates an array of 10 integers. You can also initialise inline: DECLARE days : ARRAY[0:6] OF STRING ← {‘Mon’,’Tue’,’Wed’,’Thu’,’Fri’,’Sat’,’Sun’}.

在 IB 和 CIE 伪代码中,典型的声明如下:DECLARE scores : ARRAY[0:9] OF INTEGER。这创建了一个包含 10 个整数的数组。也可以内联初始化:DECLARE days : ARRAY[0:6] OF STRING ← {‘Mon’,’Tue’,’Wed’,’Thu’,’Fri’,’Sat’,’Sun’}。

Often, exams ask you to write code to initialise an array with a formula, e.g., fill A[i] = 2*i + 1 for i from 0 to 9. A loop is required: FOR i ← 0 TO 9 then A[i] ← 2*i + 1.

考试中常常要求你编写代码,用公式初始化一个数组,例如,对于 i 从 0 到 9,填充 A[i] = 2*i + 1。需要用到循环:FOR i ← 0 TO 9,然后 A[i] ← 2*i + 1。


4. Traversing Arrays | 遍历数组

Traversal means visiting each element of an array exactly once, typically to perform an operation like output, sum, or search. The most common structure is a counted loop: FOR i ← 0 TO LENGTH(arr)-1. Inside, you reference arr[i].

遍历意味着恰好访问数组的每个元素一次,通常是为了执行如输出、求和或查找等操作。最常见的结构是计数循环:FOR i ← 0 TO LENGTH(arr)-1。循环体内部引用 arr[i]。

You must be comfortable with both forward and backward traversal (e.g., FOR i ← LENGTH(arr)-1 DOWNTO 0). Partial traversals are also common, such as processing only indices 2 to 5.

你必须熟练掌握正向和反向遍历(例如,FOR i ← LENGTH(arr)-1 DOWNTO 0)。部分遍历也很常见,比如只处理索引 2 到 5。

Avoid hard-coding the length. Use the built-in LENGTH() or UBOUND() functions provided in the exam booklet. This makes your solution general and re-usable, which earns you analysis marks.

避免硬编码长度。使用考试手册中提供的 LENGTH() 或 UBOUND() 内置函数。这使你的解决方案具有通用性和可重用性,从而获得分析分。


5. Linear Search | 线性搜索

Linear search sequentially checks each element until the target value is found or the end of the array is reached. Its time complexity is O(n) in the worst case. In pseudocode, you set a flag found ← FALSE and iterate, breaking when a match occurs.

线性搜索按顺序检查每个元素,直到找到目标值或到达数组末尾。最坏情况下的时间复杂度为 O(n)。在伪代码中,你设置一个标志 found ← FALSE 并迭代,在匹配时跳出循环。

Exam tip: always handle the case where the item is not found. A typical post-loop check: IF NOT found THEN OUTPUT “Not found”. Forgetting this is a common error.

考试技巧:始终处理未找到目标项的情况。典型的循环后检查:IF NOT found THEN OUTPUT “Not found”。忘记这一处理是常见错误。

The basic linear search can be optimised for sorted arrays by stopping early if arr[i] > target, but this is rarely required at IB/IGCSE level unless specified.

对于已排序数组,可以通过在 arr[i] > target 时提前停止来优化基本线性搜索,但这在 IB/IGCSE 级别除非特别说明,否则很少要求。


6. Binary Search | 二分搜索

Binary search works on sorted arrays by repeatedly dividing the search interval in half. Compare the target with the middle element; if not equal, discard the half that cannot contain the target. Time complexity is O(log n).

二分搜索适用于已排序数组,通过反复将搜索区间减半来工作。将目标值与中间元素比较;若不相等,则舍弃不可能包含目标值的那一半。时间复杂度为 O(log n)。

The iterative pseudocode uses low ← 0, high ← n‑1, and a while loop WHILE low ≤ high. Calculate mid ← (low + high) DIV 2. If A[mid] = target, found. If A[mid] < target, low ← mid + 1, else high ← mid - 1.

迭代伪代码使用 low ← 0、high ← n‑1,以及 while 循环 WHILE low ≤ high。计算 mid ← (low + high) DIV 2。如果 A[mid] = target,则找到。如果 A[mid] < target,low ← mid + 1,否则 high ← mid - 1。

You must be able to trace a binary search on a given dataset. IB Paper 2 loves asking for the number of comparisons made or the sequence of mid values examined.

你必须能够对给定数据集跟踪二分搜索的执行过程。IB Paper 2 喜欢要求写出比较次数或所检查的中间值序列。


7. Sorting Algorithm: Bubble Sort | 排序算法:冒泡排序

Bubble sort repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. The pass through the list is repeated until no swaps are needed. Worst-case and average complexity are O(n²).

冒泡排序反复遍历列表,比较相邻元素,如果顺序错误则交换它们。遍历列表的过程重复进行,直到没有需要交换的元素为止。最坏情况和平均复杂度均为 O(n²)。

Standard pseudocode uses nested loops: outer loop from i ← 0 TO n‑2, inner loop from j ← 0 TO n‑i‑2. Inside, IF A[j] > A[j+1] THEN SWAP A[j], A[j+1].

标准伪代码使用嵌套循环:外层循环 i ← 0 TO n‑2,内层循环 j ← 0 TO n‑i‑2。循环体内 IF A[j] > A[j+1] THEN SWAP A[j], A[j+1]。

An efficient version uses a swapped flag to exit early if the array becomes sorted before completing all passes. Mention this optimisation to demonstrate deeper understanding.

高效版本使用一个 swapped 标志,如果在完成所有遍历之前数组已排好序,则提前退出。提到这一优化可展示更深的理解。

Be able to perform a dry run on a small array (e.g., [5, 2, 8, 1]) showing the state after each pass.

要能够对一个小数组(例如 [5, 2, 8, 1])进行手动模拟,展示每趟遍历后的状态。


8. Sorting Algorithm: Insertion Sort | 排序算法:插入排序

Insertion sort builds the final sorted array one item at a time, picking each element and inserting it into its correct position relative to the already sorted prefix. It is O(n²) in the worst case but O(n) for nearly sorted data.

插入排序每次构建一个有序数组,取出每个元素并将其插入到相对于已排序前缀的正确位置。最坏情况为 O(n²),但对于近乎有序的数据为 O(n)。

The algorithm uses an outer loop FOR i ← 1 TO n‑1, storing the key temp ← A[i], then a WHILE loop shifting elements greater than temp to the right: WHILE j ≥ 0 AND A[j] > temp DO A[j+1] ← A[j]; j ← j‑1. Finally A[j+1] ← temp.

该算法使用外层循环 FOR i ← 1 TO n‑1,存储键值 temp ← A[i],然后一个 WHILE 循环将大于 temp 的元素右移:WHILE j ≥ 0 AND A[j] > temp DO A[j+1] ← A[j]; j ← j‑1。最后 A[j+1] ← temp。

Insertion sort is particularly useful in past papers when merging two sorted lists or maintaining a sorted order after each insertion.

在过往真题中,当合并两个已排序列表或在每次插入后保持有序顺序时,插入排序尤其有用。


9. Two-Dimensional Arrays | 二维数组

A 2D array can be thought of as a table with rows and columns. Declaration: DECLARE grid : ARRAY[0:3, 0:4] OF CHAR creates a 4×5 grid. The first index is the row, the second the column. In memory, they are often stored row-major.

二维数组可以看作是一个包含行和列的表格。声明:DECLARE grid : ARRAY[0:3, 0:4] OF CHAR 创建了一个 4×5 的网格。第一个索引是行,第二个是列。在内存中,它们通常按行主序存储。

Nested loops are used for processing: outer loop for rows, inner loop for columns. For example, to sum all elements: FOR r ← 0 TO 3 and inside FOR c ← 0 TO 4 accumulate grid[r][c].

使用嵌套循环进行处理:外层循环遍历行,内层循环遍历列。例如,计算所有元素之和:FOR r ← 0 TO 3,内部 FOR c ← 0 TO 4 累加 grid[r][c]。

Common exam tasks include finding the maximum in each row, transposing a matrix, or checking a 3×3 grid for magic square properties.

常见的考试任务包括查找每行的最大值、转置矩阵,或检查 3×3 网格是否具有幻方属性。


10. Arrays as Parameters | 数组作为函数参数

In pseudocode, arrays are typically passed by reference, meaning changes inside a subroutine affect the original array. The procedure header might be PROCEDURE Sort(ARRAY_A : ARRAY OF INTEGER, length : INTEGER).

在伪代码中,数组通常通过引用传递,这意味着子程序内部对数组的更改会影响原始数组。过程头可能形如 PROCEDURE Sort(ARRAY_A : ARRAY OF INTEGER, length : INTEGER)。

You may see parameters declared with empty brackets or specific bounds. Always assume the array size must be passed as a separate parameter unless a built‑in function exists.

你可能会看到用空括号或特定边界声明的参数。除非有内置函数,否则始终假定必须将数组大小作为单独参数传递。

When writing functions that return an array, ensure you create a new array inside the function and return it. The original remains unchanged if passed by value (less common).

编写返回数组的函数时,确保在函数内部创建一个新数组并返回它。如果按值传递(较少见),原数组保持不变。


11. Common Mistakes and Debugging | 常见错误与调试

Off-by-one errors: confusing <= with < in loop conditions. Always check the last iteration. Using index LENGTH(arr) instead of LENGTH(arr)-1 is the most frequent slip.

差一错误:在循环条件中混淆 <= 和 <。务必检查最后一次迭代。使用 LENGTH(arr) 而非 LENGTH(arr)-1 是最常见的失误。

Uninitialised arrays: assuming elements are 0 by default. In pseudocode, explicitly initialise if needed. In programming papers, local array variables contain garbage values.

未初始化的数组:假设元素默认为 0。在伪代码中,如有需要应显式初始化。在编程考试中,局部数组变量包含垃圾值。

Misunderstanding zero-based indexing: when the question uses 1‑based (e.g., subject codes), adapt your loop boundaries accordingly. Dry running with small arrays helps catch these.

误解基于零的索引:当题目使用基于 1 的索引(如科目代码)时,相应地调整循环边界。用小数组进行手动模拟有助于发现这些错误。


12. Exam-Style Question Breakdown | 真题示例解析

Question (typical CIE Paper 4): Write a procedure that takes an array of 20 integers and outputs the index of the smallest value. If multiple occurrences exist, output the first one.

题目(典型 CIE Paper 4):编写一个过程,接收一个包含 20 个整数的数组,输出最小值的索引。如果存在多个最小值,输出第一个。

Solution approach: Initialise minIndex ← 0, loop i from 1 to 19. If A[i] < A[minIndex], update minIndex. Finally output minIndex. This is a classic linear scan.

解题思路:初始化 minIndex ← 0,从 1 到 19 循环。如果 A[i] < A[minIndex],更新 minIndex。最后输出 minIndex。这是一个经典的线性扫描。

Mark scheme focus: correct loop bounds (1 to 19, not 0 to 19), correct comparison, and proper output. Many lost marks by looping from 0 and comparing with itself unnecessarily, or forgetting to initialise.

评分标准关注点:正确的循环边界(1 到 19,而非 0 到 19),正确的比较,以及适当的输出。许多失分是因为从 0 开始循环并与自身进行不必要的比较,或者忘记初始化。

For IB, be prepared to trace a 2D array algorithm and to identify the purpose of a given pseudocode snippet that manipulates arrays.

对于 IB,准备好跟踪二维数组算法,并识别给定伪代码片段中操作数组的目的。

Published by TutorHao | Computer Science Revision Series | aleveler.com

更多咨询请联系16621398022(同微信)

Comments

屏轩国际教育cambridge primary/secondary checkpoint, cat4, ukiset,ukcat,igcse,alevel,PAT,STEP,MAT, ibdp,ap,ssat,sat,sat2课程辅导,国外大学本科硕士研究生博士课程论文辅导Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from aleveler.com

Subscribe now to keep reading and get access to the full archive.

Continue reading

Exit mobile version