数组
引入
17:29
数据类型与结构
程序存储的每个值都需要一个数据类型——选择合适的类型至关重要。假设你存储某商品是否有库存。你可以写单词 yes……
英文讲解 · 可切换中英文字幕
本课内容
| English | 释义 | 拼音 |
|---|---|---|
| variables/ˈveərɪəblz/ | 变量 | biàn liàng |
| array/əˈreɪ/ | 数组 | shù zǔ |
| row/rəʊ/ | 行 | xíng |
| algorithm/ˈælɡərɪθəm/ | 算法 | suàn fǎ |
| declarations | 声明 | shēng míng |
| pseudocode/ˈsuːdəʊkəʊd/ | 伪代码 | wěi dài mǎ |
| data structure/ˈdeɪtə ˈstrʌktʃə/ | 数据结构 | shù jù jié gòu |
| data type/ˈdeɪtə taɪp/ | 数据类型 | shù jù lèi xíng |
| identifier/aɪˈdentɪfaɪə/ | 标识符 | biāo shí fú |
| index/ˈɪndeks/ | 索引 | suǒ yǐn |
| lower bound/ˈləʊə baʊnd/ | 下界 | xià jiè |
| upper bound/ˈʌpə baʊnd/ | 上界 | shàng jiè |
| element/ˈelɪmənt/ | 元素 | yuán sù |
| dimension/daɪˈmenʃn/ | 维度 | wéi dù |
| column/ˈkɒlʌm/ | 列 | liè |
| Nested loops/ˈnestɪd luːps/ | 嵌套循环 | qiàn tào xún huán |
| outer loop/ˈaʊtə luːp/ | 外层循环 | wài céng xún huán |
| linear search/ˈlɪnɪə sɜːtʃ/ | 线性查找 | xiàn xìng chá zhǎo |
| Initialise/ɪˈnɪʃəlaɪz/ | 初始化 | chū shǐ huà |
| bubble sort/ˈbʌbl sɔːt/ | 冒泡排序 | mào pào pái xù |
| swap/swɒp/ | 交换 | jiāo huàn |
| flag/flæɡ/ | 标志 | biāo zhì |
| temporary variable | 临时变量 | lín shí biàn liàng |
| stepwise-refinement | 逐步求精 | zhú bù qiú jīng |
| sorted array | 有序数组 | yǒu xù shù zǔ |
| overwrite/ˌəʊvəˈraɪt/ | 覆盖 | fù gài |
14 排 C 座
- 一家电影院有 300 个座位。它的订票系统没有 300 个叫
Seat1A、Seat1B、Seat1C的变量。它有一个数组,你的票指向数组中的一个地址:第 14 排,C 座。 - 一个名字,几百个值,每个都靠数字找到。加一排座位,代码不用改;循环遍历这些数字,就检查了每个座位。
- 第二试卷上几乎每个算法都要遍历数组:查找、求和、排序、找最大值。
- 这一课讲术语、声明,以及考官要求用伪代码和文字写出的四个算法。

术语
- 数组是在一个标识符下保存固定数量同一数据类型的元素的数据结构,每个元素通过索引访问。
- 下界和上界是第一个和最后一个有效索引。元素个数是上界 − 下界 + 1。
- 维度是一个元素需要的索引个数:列表需要一个,表格需要两个。
- 在
ThisArray[n] ← 42中,数组是一维的,索引是INTEGER变量n,该索引处的元素接收42。

一个标识符,每个元素一个索引,两端各有边界
An array stores:
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
An array is an ordered collection of same-type items accessed by index. (A record groups different types.)
An array is a data structure holding many values of the ______ type under one name.
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
Each value is reached by its index.
DECLARE Marks : ARRAY[0:99] OF INTEGER declares an array of ____ elements.
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
Upper bound minus lower bound plus one: 99 − 0 + 1 = 100. Both bounds are valid indices.
例题:声明任务需要的数组
- 声明需要标识符、边界和数据类型。
- 120 个可能带小数的读数:
DECLARE Data : ARRAY[1:120] OF REAL - 150 行两列的文本表:
DECLARE Names : ARRAY[1:150, 1:2] OF STRING - 被问到时说出个数:
[0:99]保存 100 个元素,不是 99 个。
Which declaration holds a table of 150 rows and 2 columns of text?
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
Two dimensions, each with a lower and upper bound, and the element type. The second option is one long list; the third has no type; the fourth has no lower bounds.
处理一维数组
DECLARE Names : ARRAY[1:5] OF STRING
Names[3] ← "Cara"
FOR i ← 1 TO 5
OUTPUT Names[i]
NEXT i
- 从下界到上界的
FOR循环把每个元素访问一次。 - 求和、计数、求最大值或最小值时,在循环前设置一个变量,并在循环内更新它。
二维数组
DECLARE Grid : ARRAY[1:3, 1:4] OF INTEGER
Grid[2, 3] ← 99 // row 2, column 3
- 第一个索引是行,第二个是列。嵌套循环访问每个单元格:外层循环遍历行,内层循环遍历列。
- 单个序列使用一维数组;数据具有两个自然维度时使用二维数组,例如座位网格或按学生和科目排列的成绩表。

Grid[row, column],顺序始终如此
Index a 2-D array by [row, column]
A 2-D array is a grid. Grid[row, column] reaches exactly one cell — change the row and column to see which value you land on.
In Grid[2, 3], which cell is accessed?
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
The first index is the row, the second the column — so row 2, column 3.
例题:能够报告“未找到”的线性查找
- 线性查找依次检查元素;下面的例子会检查整个数组,如果有匹配项,就记录最后一个匹配项的索引。
FoundAt ← -1
FOR i ← 1 TO n
IF A[i] = Target THEN
FoundAt ← i
ENDIF
NEXT i
IF FoundAt = -1 THEN
OUTPUT "Not found"
ELSE
OUTPUT "Found at ", FoundAt
ENDIF
-1不在1到n的索引范围内,因此在这个例子中表示“未找到”。在循环之前将它初始化,并在循环之后检查它。查找程序如果从不报告“未找到”,就会丢一分。
A linear search finds a value by:
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
A linear search examines elements one by one from the start until it finds the target (or reaches the end).
Setting FoundAt to -1 before a linear search lets the program report "not found" after the loop.
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
For an array indexed from 1 to n, -1 is not a valid index, so if it is unchanged after the loop the target was not in the array.
最大值及其位置
Largest ← A[1]
Position ← 1
FOR i ← 2 TO n
IF A[i] > Largest THEN
Largest ← A[i]
Position ← i
ENDIF
NEXT i
OUTPUT Largest, " at ", Position
- 将
Largest的初始值设为第一个元素,绝不设为 0:数组中的数可能全是负数。 - 同样的结构可以对非空元素计数或输出:把每个元素与未使用标记(
""或-1)比较,只对不同的元素计数或输出。
冒泡排序
- 冒泡排序多遍遍历数组,比较相邻的元素对,并交换顺序错误的元素,直到某一遍没有发生交换。
- 每遍之后,最大的未排序值都已冒到末尾,所以下一遍可以提前一个位置结束。
Limit ← n // n is the number of elements in Data
REPEAT
Swapped ← FALSE
FOR Index ← 1 TO Limit - 1
IF Data[Index] > Data[Index + 1] THEN
Temp ← Data[Index]
Data[Index] ← Data[Index + 1]
Data[Index + 1] ← Temp
Swapped ← TRUE
ENDIF
NEXT Index
Limit ← Limit - 1
UNTIL Swapped = FALSE

每一遍都将剩余的最大值移到末尾
Put the steps of one bubble-sort pass, and its ending, in order.
难度 3/5 · 今日首次完成可获 200 金币及少量额外奖励
Reset the flag, sweep and swap, shrink the limit, stop when a whole pass made no swap.
例题:冒泡排序的得分点
- 外层循环重复执行,直到某一遍没有交换;
Swapped标志在每遍开始时重置为FALSE,并在IF内设为TRUE。 - 通过临时变量进行的三行交换。只用两行会丢失一个值。
- 递减的上限,每遍减一,因为最大值已经到了末尾。
- 用文字回答逐步求精题:重复直到有序;每遍比较相邻的元素对;交换顺序错误的元素对;每遍之后,最大的未排序值都在末尾。

Which features earn marks in an efficient bubble sort? Select 所有 that apply.
难度 2/5 · 今日首次完成可获 140 金币及少量额外奖励
Flag, swap with a temporary, shrinking limit: those are the marks. Copying the array is not part of the algorithm.
例题:删除和插入
- 删除一项:用线性查找找到它的索引;把后面的每个元素向前移一位,填上空隙;把最后一个元素标记为未使用,或减少计数。
- 插入到有序数组:找到第一个元素比新值大的索引;从最后一个元素开始,把该元素及其后面的每个元素向后移一位;把新值存入空隙。
- 腾出空隙时从末尾开始移动,填补空隙时从开头开始移动,否则会覆盖即将移动的值。

An array holds many items of the SAME type reached by index, while a record groups fields of (possibly) DIFFERENT types reached by name.
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
A 2-D array suits a grid (rows × columns); a record suits one thing described by several named fields.
容易丢掉的分
[0:99]保存 100 个元素。两端的边界都要算。- 索引是
INTEGER;声明既要指定边界,也要指定类型。 Grid[row, column]:行在前。若交换顺序,每个嵌套循环都会读取错误的单元格。- 交换可以使用临时变量;查找需要有“未找到”的处理路径;冒泡排序在某一遍没有交换时可以提前结束。
你掌握了
- 数组在一个标识符下保存固定数量同一类型的元素,通过下界和上界之间的索引访问;个数 = 上界 − 下界 + 1
- 1-D 是列表,2-D 是由嵌套循环遍历的表格
[row, column];声明时需指定边界和类型 - 线性查找:
FoundAt ← -1,循环,存下索引,循环后测试;最大值:从A[1]开始,记住位置 - 冒泡排序:相邻比较并用临时变量交换的多遍、一个
Swapped标志、递减的上限,直到某一遍没有交换
出了什么问题?只有网站团队能看到这些信息。