📚 IGCSE Computer Science: Arrays – Key Points | IGCSE 计算机:数组 考点精讲
Arrays are one of the fundamental data structures in IGCSE Computer Science. They allow a programmer to store multiple values of the same data type under a single identifier, making it easier to manage, search, and sort data efficiently. This article covers essential array concepts, including declaration, indexing, traversal, search algorithms (linear search), sorting algorithms (bubble sort and selection sort), and two-dimensional arrays. Understanding arrays is crucial for success in Paper 2 (Algorithms, Programming and Logic) and for practical programming tasks.
数组是 IGCSE 计算机科学中基础的数据结构之一。它允许程序员使用单个标识符存储多个相同数据类型的值,从而更容易高效地管理、搜索和排序数据。本文涵盖数组的核心概念,包括声明、索引、遍历、搜索算法(线性搜索)、排序算法(冒泡排序和选择排序)以及二维数组。掌握数组对于在 Paper 2(算法、编程与逻辑)中取得成功以及完成实际编程任务至关重要。
1. What is an Array? | 什么是数组?
An array is a data structure that can hold a fixed number of elements, all of the same data type, stored in contiguous memory locations. Each element can be accessed using an index (or subscript). Arrays are useful for working with collections of data such as lists of numbers, strings, or records.
数组是一种数据结构,可以容纳固定数量的元素,所有元素具有相同的数据类型,存储在连续的内存位置中。每个元素可以通过索引(下标)访问。数组适用于处理数据集合,如数字列表、字符串或记录。
2. Declaring and Initializing Arrays | 数组的声明与初始化
In pseudocode or high-level programming languages, you must declare an array before using it. Declaration includes the array name and size. For example, using IGCSE-style pseudocode: DECLARE scores : ARRAY[1:10] OF INTEGER. This creates an array of ten integers with indices from 1 to 10. In Python, you might use scores = [0]*10 or scores = [None]*10.
在伪代码或高级编程语言中,使用数组前必须先声明。声明包括数组名称和大小。例如,使用 IGCSE 风格的伪代码:DECLARE scores : ARRAY[1:10] OF INTEGER。这将创建一个包含十个整数的数组,索引从 1 到 10。在 Python 中,可以使用 scores = [0]*10 或 scores = [None]*10。
Initialization sets the initial values. You can assign values individually: scores[1] ← 85, scores[2] ← 90, or use a loop to fill the array with a default value. Uninitialized arrays may contain garbage data if not set explicitly.
初始化设置初始值。可以逐个赋值:scores[1] ← 85、scores[2] ← 90,或者使用循环填充默认值。如果不显式设置,未初始化的数组可能包含垃圾数据。
3. Accessing Array Elements (Indexing) | 访问数组元素(索引)
Array elements are accessed by placing the index inside square brackets: arrayName[index]. In IGCSE pseudocode, indices typically start at 1, so scores[3] refers to the third element. In many programming languages like Python and Java, indices start at 0, so caution is needed when translating pseudocode into actual code.
数组元素通过将索引放在方括号内来访问:arrayName[index]。在 IGCSE 伪代码中,索引通常从 1 开始,所以 scores[3] 指第三个元素。在 Python 和 Java 等许多编程语言中,索引从 0 开始,因此在将伪代码转换为实际代码时需要小心。
The index must be a non-negative integer expression. Variables can be used as indices, e.g., scores[i] where i is a loop counter. Accessing an index outside the declared bounds results in an ‘index out of bounds’ error.
索引必须是非负整数表达式。可以使用变量作为索引,例如 scores[i],其中 i 是循环计数器。访问超出声明边界的索引会导致“索引越界”错误。
4. Modifying Array Elements | 修改数组元素
You can modify a specific array element by assigning a new value: marks[5] ← 78. After this statement, the element at index 5 now holds 78, overwriting any previous value. This is often used to update records, scores, or sensor readings.
可以通过赋值修改特定的数组元素:marks[5] ← 78。执行该语句后,索引 5 处的元素现在保存 78,覆盖之前的任何值。这常用于更新记录、分数或传感器读数。
Combining indexing with arithmetic is common: prices[i] ← prices[i] * 1.1 applies a 10% increase to one element. Be careful with index ranges: if the array size is 10, valid indices are 1–10 (pseudocode) or 0–9 (Python).
将索引与算术结合很常见:prices[i] ← prices[i] * 1.1 对一个元素应用 10% 的涨幅。注意索引范围:如果数组大小为 10,有效索引为 1–10(伪代码)或 0–9(Python)。
5. Traversing an Array with a Loop | 使用循环遍历数组
Traversing means visiting each element of an array, usually to perform an operation like printing, summing, or searching. The most common method is a FOR loop that runs from the first index to the last index. Pseudocode:
遍历意味着访问数组的每个元素,通常用于执行打印、求和或搜索等操作。最常见的方法是使用 FOR 循环,从第一个索引运行到最后一个索引。伪代码:
FOR i ← 1 TO LENGTH(arr)
OUTPUT arr[i]
NEXT i
In Python, you can use a for loop directly over the elements: for item in arr: print(item), or with an index: for i in range(len(arr)): print(arr[i]).
在 Python 中,可以直接对元素使用 for 循环:for item in arr: print(item),或使用索引:for i in range(len(arr)): print(arr[i])。
When writing the loop condition, always ensure the counter stays within the array bounds. Hardcoding the upper bound (e.g., FOR i ← 1 TO 10) is less flexible than using LENGTH(arr) because the array size might change.
编写循环条件时,务必确保计数器保持在数组边界内。硬编码上界(例如 FOR i ← 1 TO 10)不如使用 LENGTH(arr) 灵活,因为数组大小可能会改变。
6. Linear Search in an Array | 数组中的线性搜索
Linear search is the simplest searching algorithm. It examines each element in order until the target value is found or the end of the array is reached. It works on both sorted and unsorted arrays but has a time complexity of O(n) in the worst case.
线性搜索是最简单的搜索算法。它按顺序检查每个元素,直到找到目标值或到达数组末尾。它适用于已排序和未排序的数组,但最坏情况的时间复杂度为 O(n)。
IGCSE pseudocode for linear search:
DECLARE arr : ARRAY[1:8] OF INTEGER
DECLARE searchValue, index, found : INTEGER
found ← FALSE
index ← 1
WHILE index <= LENGTH(arr) AND found = FALSE DO
IF arr[index] = searchValue THEN
found ← TRUE
OUTPUT 'Found at index ', index
ELSE
index ← index + 1
ENDIF
ENDWHILE
IF found = FALSE THEN
OUTPUT 'Not found'
ENDIF
IGCSE 线性搜索伪代码如上。该算法维护一个标志 'found' 来指示是否已找到目标。当找到时循环提前结束;否则 index 递增。对于考试,你需要能够手写该算法。
In Python, a linear search can be implemented as:
def linear_search(arr, target):
for i in range(len(arr)):
if arr[i] == target:
return i
return -1
Python 实现:遍历索引并比较,找到则返回索引,否则返回 -1。
7. Bubble Sort Algorithm | 冒泡排序算法
Bubble sort is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. After each complete pass, the largest unsorted element 'bubbles up' to its correct position at the end of the array. It stops when no swaps are made in a pass.
冒泡排序是一种简单的排序算法,它反复遍历列表,比较相邻元素,如果顺序错误则交换它们。每完成一趟后,最大的未排序元素“冒泡”到数组末尾的正确位置。当一趟中没有发生交换时停止。
Pseudocode (sorting an array in ascending order):
DECLARE arr : ARRAY[1:n] OF INTEGER
DECLARE i, j, temp : INTEGER
DECLARE swapped : BOOLEAN
FOR i ← 1 TO n-1
swapped ← FALSE
FOR j ← 1 TO n-i
IF arr[j] > arr[j+1] THEN
temp ← arr[j]
arr[j] ← arr[j+1]
arr[j+1] ← temp
swapped ← TRUE
ENDIF
NEXT j
IF swapped = FALSE THEN
EXIT FOR
ENDIF
NEXT i
伪代码(升序排序)如上。外循环控制趟数,内循环进行相邻比较和交换。使用 'swapped' 标志允许提前终止,提高最佳情况下的效率。时间复杂度为 O(n²)。
In Python, a typical implementation:
def bubble_sort(arr):
n = len(arr)
for i in range(n-1):
swapped = False
for j in range(n-1-i):
if arr[j] > arr[j+1]:
arr[j], arr[j+1] = arr[j+1], arr[j]
swapped = True
if not swapped:
break
Python 实现使用相同的逻辑。注意 Python 的并行赋值简化了交换操作。
8. Selection Sort Algorithm | 选择排序算法
Selection sort divides the array into a sorted part (left) and an unsorted part (right). In each iteration, it finds the smallest element in the unsorted part and swaps it with the first element of the unsorted part, thus extending the sorted portion. It performs fewer swaps than bubble sort but still has O(n²) time complexity.
选择排序将数组分为已排序部分(左侧)和未排序部分(右侧)。每次迭代中,找出未排序部分中的最小元素,并将其与未排序部分的第一个元素交换,从而扩展已排序部分。它比冒泡排序的交换次数少,但时间复杂度仍为 O(n²)。
Pseudocode for selection sort (ascending):
DECLARE arr : ARRAY[1:n] OF INTEGER
DECLARE i, j, minIndex, temp : INTEGER
FOR i ← 1 TO n-1
minIndex ← i
FOR j ← i+1 TO n
IF arr[j] < arr[minIndex] THEN
minIndex ← j
ENDIF
NEXT j
temp ← arr[i]
arr[i] ← arr[minIndex]
arr[minIndex] ← temp
NEXT i
选择排序(升序)伪代码:外循环从 1 到 n-1,内循环在剩余部分中查找最小值的索引,然后交换。与冒泡排序不同,它总是执行 n-1 次交换(每趟一次)。
Python implementation:
def selection_sort(arr):
n = len(arr)
for i in range(n-1):
min_index = i
for j in range(i+1, n):
if arr[j] < arr[min_index]:
min_index = j
arr[i], arr[min_index] = arr[min_index], arr[i]
Python 代码结构清晰,注意 min_index 的查找和交换操作。
9. Two-Dimensional Arrays | 二维数组
A two-dimensional (2D) array is an array of arrays, organized in rows and columns. It is declared with two dimensions: DECLARE grid : ARRAY[1:3, 1:4] OF INTEGER creates a 3-row, 4-column matrix. Elements are accessed using two indices: grid[row, column].
二维数组是数组的数组,按行和列组织。它用两个维度声明:DECLARE grid : ARRAY[1:3, 1:4] OF INTEGER 创建一个 3 行 4 列的矩阵。元素通过两个索引访问:grid[row, column]。
Common uses include game boards (tic-tac-toe, chess), spreadsheets, and matrices in mathematics. To traverse a 2D array, you need nested loops – an outer loop for rows and an inner loop for columns:
FOR row ← 1 TO 3
FOR col ← 1 TO 4
OUTPUT grid[row, col]
NEXT col
NEXT row
常见用途包括游戏棋盘(井字棋、国际象棋)、电子表格和数学中的矩阵。遍历二维数组需要使用嵌套循环——外循环处理行,内循环处理列。
In Python, a 2D list is used: grid = [[0]*4 for _ in range(3)]. Access is grid[row][col] with indices 0–2 and 0–3. Be careful: the first index selects the row, the second the column.
在 Python 中使用二维列表:grid = [[0]*4 for _ in range(3)]。访问为 grid[row][col],索引范围 0–2 和 0–3。注意:第一个索引选择行,第二个选择列。
10. Common Errors and Pitfalls | 常见错误与陷阱
Several mistakes frequently appear in IGCSE array questions. Avoid these to secure marks:
在 IGCSE 数组问题中经常出现一些错误。避免这些错误以确保得分:
- Index out of bounds: Using an index less than the lower bound (e.g., 0 in pseudocode) or greater than the array size. Always check loop conditions.
- Index out of bounds: 使用小于下界(例如伪代码中的 0)或大于数组大小的索引。务必检查循环条件。
- Uninitialized elements: Reading array elements before they have been assigned a value can produce unexpected results.
- 未初始化元素:在赋值之前读取数组元素可能产生意外结果。
- Off-by-one errors: In FOR loops, using
FOR i ← 1 TO n-1when you meantFOR i ← 1 TO n, or vice versa, especially in sorting algorithms. - 差一错误:在 FOR 循环中,本意是
FOR i ← 1 TO n却写成FOR i ← 1 TO n-1,反之亦然,尤其在排序算法中。 - Confusing row and column in 2D arrays: Ensure the first index corresponds to the row and the second to the column.
- 混淆二维数组的行与列:确保第一个索引对应行,第二个对应列。
- Incorrect use of search/sort algorithms: Forgetting to reset the 'found' flag, or not updating loop counters correctly in a linear search.
- 搜索/排序算法使用不当:忘记重置“found”标志,或在线性搜索中未正确更新循环计数器。
- Inefficient bubble sort without swap flag: Missing the swapped flag can make the algorithm unnecessarily slow in some cases.
- 冒泡排序缺少交换标志:缺少 swapped 标志可能使算法在某些情况下不必要地变慢。
11. Summary of Key Points | 考点总结
Arrays store multiple values of the same type, accessed by index. In IGCSE pseudocode, indices usually start at 1 and you must declare the array size explicitly. Traversal with a FOR loop enables bulk processing. Linear search is a fundamental algorithm with O(n) complexity; bubble sort and selection sort are O(n²) sorting algorithms that must be understood step by step. Two-dimensional arrays model tabular data using row and column indices. Focus on writing clear pseudocode for each algorithm and practice translating it into a programming language you are familiar with.
数组存储多个相同类型的值,通过索引访问。在 IGCSE 伪代码中,索引通常从 1 开始,并且必须显式声明数组大小。使用 FOR 循环遍历数组可进行批量处理。线性搜索是基础算法,复杂度为 O(n);冒泡排序和选择排序是 O(n²) 的排序算法,需要逐步理解。二维数组使用行索引和列索引模拟表格数据。重点是为每个算法编写清晰的伪代码,并练习将其转换为你熟悉的编程语言。
Published by TutorHao | IGCSE Computer Science Revision Series | aleveler.com
更多咨询请联系16621398022(同微信)
屏轩国际教育cambridge primary/secondary checkpoint, cat4, ukiset,ukcat,igcse,alevel,PAT,STEP,MAT, ibdp,ap,ssat,sat,sat2课程辅导,国外大学本科硕士研究生博士课程论文辅导