跳到主要内容

计算思维与问题求解

A-Level 计算机科学知识点 19

本主题的视频课程 打开视频页面
15:34

搜索与排序

一本包含一百万条记录的电话簿。如果逐一查找,可能需要一百万次比较。但你已经知道窍门:翻开它到……

英文讲解 · 可切换中英文字幕

19.1

算法

教学大纲
考生应能: 注释与指导
展示对线性搜索和二分搜索方法的理解 编写实现线性搜索的算法
编写实现二分搜索的算法
使用二分搜索所需的条件
二分搜索的性能如何随数据项数量变化
展示对插入排序和冒泡排序方法的理解 编写实现插入排序的算法
编写实现冒泡排序的算法
排序例程的性能可能取决于数据的初始顺序和数据项的数量
展示对**抽象数据类型(ADT)**的理解并加以运用 编写在以下数据结构中查找某项的算法:链表、二叉树
编写向以下数据结构中插入某项的算法:栈、队列、链表、二叉树
编写从以下数据结构中删除某项的算法:栈、队列、链表
展示理解图是ADT的一个示例。描述图的关键特征,并为特定场景论证其使用的合理性。考生无需为图结构编写代码
展示ADT可以由另一种ADT实现 描述以下ADT,并演示如何利用适当的内置类型或其他ADT对其进行实现:栈、队列、链表、字典、二叉树
展示理解:执行相同任务的不同算法可以通过标准(例如完成任务所需的时间和内存占用)进行比较 包括使用大O记号来指定时间复杂度和空间复杂度

来源:Cambridge International 教学大纲

English
Big O: how algorithms scale
Insertion sort: slide each card into place
Bubble sort, pass by pass
Binary search: halve and conquer

A search finds a target value in a collection (often an array 数组) and returns its position, or “not found”.

An open telephone directory
Searching a sorted list, like a phone book, is far faster than checking every entry one by one

Linear search

A linear search 线性查找 walks from start to end, comparing each element with the target:

FOR i ← 1 TO n
    IF A[i] = target THEN
        RETURN i
    ENDIF
NEXT i
RETURN -1   // not found

No preparation is needed, so it works on any list. Worst case O($n$) (target at the end or absent); best case 1 comparison. Use it on unsorted data or small lists. (The returned -1 is a sentinel value — an impossible position that means “not found”; the caller tests IF result = -1.)

The exam's version. Paper 3 asks you to complete a linear search written with a flag and a WHILE loop, and Paper 4 to write a function that returns the index or a count. Both look like this:

FUNCTION LinearSearch(Data : ARRAY OF INTEGER, Target : INTEGER) RETURNS INTEGER
    DECLARE Index, Count : INTEGER
    Count ← 0
    FOR Index ← 1 TO 100
        IF Data[Index] = Target THEN
            Count ← Count + 1
        ENDIF
    NEXT Index
    RETURN Count          // how many times Target occurs; 0 means not found
ENDFUNCTION

To stop at the first match instead, use a WHILE Index <= 100 AND NOT Found loop that sets Found ← TRUE and remembers the index. The marks are for the loop over every element, the comparison, and what is returned when the value is absent.

A row of alphabet cells A to Z; cells A to V are shaded as checked and W is highlighted as the match, with a pointer below W
Linear search checks every letter in turn — 23 comparisons to find W

Binary search

A binary search 二分查找 needs the data sorted. Look at the middle element; if it is the target, done; if the target is smaller, search the left half, else the right half — halving the range each time:

low ← 1
high ← n
WHILE low <= high DO
    MiddleIndex ← (low + high) DIV 2
    IF A[MiddleIndex] = target THEN
        RETURN MiddleIndex
    ENDIF
    IF A[MiddleIndex] < target THEN
        low ← MiddleIndex + 1
    ELSE
        high ← MiddleIndex - 1
    ENDIF
ENDWHILE
RETURN -1

Worst case O($\log_{2} n$) — for a million items, about 20 comparisons. Much faster than linear search on large sorted arrays, but you must sort first (a one-off O($n \log n$) cost), worth it if you search many times.

“State the condition necessary for a binary search.” The data must be in order (sorted, ascending or descending, on the key being searched). “Describe how to perform a binary search” (three marks): (1) find the middle item of the list (or of the current range) and compare it with the target; (2) if it matches, the search ends; if the data is ascending and the target is smaller, repeat on the lower half, if larger, on the upper half; reverse these directions for descending data; (3) keep halving the range until the item is found or the range is empty, which means it is not present.

The exam's version, with the bounds and a flag, is the one to reproduce when asked to complete the algorithm:

DECLARE Lower, Upper, MiddleIndex : INTEGER
DECLARE Found : BOOLEAN
Lower ← 0
Upper ← 99
Found ← FALSE
WHILE Lower <= Upper AND NOT Found
    MiddleIndex ← (Lower + Upper) DIV 2
    IF Names[MiddleIndex] = Target THEN
        Found ← TRUE
    ELSE
        IF Names[MiddleIndex] < Target THEN
            Lower ← MiddleIndex + 1
        ELSE
            Upper ← MiddleIndex - 1
        ENDIF
    ENDIF
ENDWHILE
IF Found THEN
    OUTPUT MiddleIndex
ELSE
    OUTPUT "Not found"
ENDIF

Worked example (tracing a binary search). Names[0:9] holds Ali, Ben, Cai, Dev, Eva, Fay, Gus, Hal, Ivy, Jo. Trace the exam's version for Target = "Ivy", then for Target = "Cat", giving Lower, Upper and MiddleIndex at each step.

For “Ivy”:

Lower Upper MiddleIndex Names[MiddleIndex] Result
0 9 4 Eva Eva < Ivy, so Lower ← 5
5 9 7 Hal Hal < Ivy, so Lower ← 8
8 9 8 Ivy Found ← TRUE

The output is 8, after three comparisons. For “Cat”: middle 4 (Eva > Cat, Upper ← 3); middle 1 (Ben < Cat, Lower ← 2); middle 2 (Cai < Cat, Lower ← 3); middle 3 (Dev > Cat, Upper ← 2). Now Lower (3) is greater than Upper (2), so the loop ends and “Not found” is output after four comparisons, the worst case for ten items ($\lceil\log_2 10\rceil = 4$). DIV rounds down, so the middle of 0 and 9 is 4, not 4.5 or 5. Strings compare in alphabetical (character code) order.

“Explain how the performance varies with the number of items.” Each comparison halves the number of items left, so the maximum number of comparisons is about $\log_{2} n$: doubling the size of the list adds only one more comparison. This is O($\log n$). “Compare linear and binary search”: a linear search needs up to $n$ comparisons (O($n$)) and, on average, half that, but works on unsorted data; a binary search needs at most $\lfloor\log_{2} n\rfloor + 1$ comparisons for $n > 0$ (O($\log n$)) and is far faster for large lists, but the data must first be sorted and it must allow direct access to the middle item (an array, not a linked list). For $1000$ items: $1000$ against $10$ comparisons.

Three rows showing binary search on the sorted alphabet; the active low-to-high range halves each step as the middle letter M, then T, then W is compared with W
Binary search halves the range each step (low / mid / high) — just 3 comparisons to find W
A library card catalogue: a wall of small wooden drawers, one pulled open to show the cards filed in order
A card catalogue: sorted records are what make a binary search possible — halve, look, halve again
探索

线性 vs 二分查找

查找一个值。二分搜索每一步将列表减半(仅适用于有序数据);线性搜索逐个检查。

词汇 训练
English 中文 拼音
binary search/ˈbaɪnəri sɜːtʃ/ 二分查找 èr fēn chá zhǎo
array/əˈreɪ/ 数组 shù zǔ
linear search/ˈlɪnɪə sɜːtʃ/ 线性查找 xiàn xìng chá zhǎo
19.1

排序算法

English

Bubble sort

A bubble sort 冒泡排序 repeatedly walks the array, swapping adjacent pairs that are out of order, so the largest “bubbles” to the end each pass:

FOR pass ← 1 TO n - 1
    swapped ← FALSE
    FOR i ← 1 TO n - pass
        IF A[i] > A[i + 1] THEN
            temp ← A[i]
            A[i] ← A[i + 1]
            A[i + 1] ← temp
            swapped ← TRUE
        ENDIF
    NEXT i
    IF swapped = FALSE THEN      // already sorted
        EXIT FOR
    ENDIF
NEXT pass

Best case O($n$) (already sorted, with the early exit); average/worst O($n^{2}$). Simple but slow for large $n$.

Insertion sort

An insertion sort 插入排序 builds a sorted prefix from the left, inserting each new element into place by shifting larger ones right:

FOR i ← 2 TO n
    key ← A[i]
    j ← i - 1
    WHILE j >= 1 AND A[j] > key DO
        A[j + 1] ← A[j]
        j ← j - 1
    ENDWHILE
    A[j + 1] ← key
NEXT i

Best case O($n$) (already sorted); worst O($n^{2}$). Good for small or nearly-sorted arrays. It sorts in place 原地 and is stable 稳定 (keeps the order of equal elements).

Tracing a sort

A common task is to show the array after each outer pass. For [D, T, H, R] with insertion sort: pass 1 (key T) no change; pass 2 (key H) → [D, H, T, R]; pass 3 (key R) → [D, H, R, T].

Insertion-sort snapshots
Insertion sort on the course dataset: each row is one insertion; the shaded prefix is already sorted

The same array with bubble sort moves differently: pass 1 compares D/T (no swap), T/H (swap) and T/R (swap), giving [D, H, R, T] with the largest item at the end; pass 2 makes no swaps, so the flag stops the sort. Show the whole array after each pass, and count the comparisons if asked: $3$ in pass 1 and $2$ in pass 2 (the version above stops one place earlier each pass), $5$ in all.

Writing a sort from scratch. “Write pseudocode to sort DataArray[1:1000] into ascending order” is answered by a complete bubble sort with the early-exit flag, or an insertion sort, declared and indented; either scores full marks if it works for every input:

DECLARE Pass, Index, Temp : INTEGER
DECLARE Swapped : BOOLEAN
Pass ← 1
REPEAT
    Swapped ← FALSE
    FOR Index ← 1 TO 1000 - Pass
        IF DataArray[Index] > DataArray[Index + 1] THEN
            Temp ← DataArray[Index]
            DataArray[Index] ← DataArray[Index + 1]
            DataArray[Index + 1] ← Temp
            Swapped ← TRUE
        ENDIF
    NEXT Index
    Pass ← Pass + 1
UNTIL Swapped = FALSE OR Pass = 1000

For descending order change > to <; to sort records or a 2D array by one field, compare that field but swap the whole record (or every column). Asked to write an insertion sort “that performs the same task” as a given bubble sort, keep the same array name and direction and reproduce the insertion sort above with the comparison reversed if the order is descending.

“Describe two ways the performance of a sort is affected by the data” (two marks). (1) The number of items: an $O(n^{2})$ sort takes four times as long for twice as many items. (2) How far the data is already in order: a bubble sort with a flag, or an insertion sort, finishes in one pass over already-sorted data ($O(n)$) and does the most work on data in reverse order; the number of swaps depends on how many pairs are out of order. (Also accepted: the range or number of duplicate values, and whether the items are large records that are expensive to move.) Bubble and insertion sort are both O($n^{2}$) in the worst and average cases and O($n$) at best; merge sort is O($n \log n$), while quicksort is O($n \log n$) on average and O($n^{2}$) in the worst case.

Rows tracing an insertion sort of D, T, H, R across three passes; the sorted prefix is shaded and arrows show each larger element shifting right to let the key drop in
An insertion sort of [D, T, H, R], shifting each key into its place pass by pass
探索

看排序的过程

逐步执行排序算法,观察条形图逐步归位——排序算法如何逐轮工作。

词汇 训练
English 中文 拼音
insertion sort/ɪnˈsɜːʃn sɔːt/ 插入排序 chā rù pái xù
bubble sort/ˈbʌbl sɔːt/ 冒泡排序 mào pào pái xù
in place/ɪn pleɪs/ 原地 yuán dì
stable/ˈsteɪbl/ 稳定 wěn dìng
19.1

算法中的抽象数据类型

English

The Abstract Data Types (ADTs) from Topic 10 appear inside many algorithms: a stack 栈 drives depth-first traversal and undo; a queue 队列 drives breadth-first traversal and print ordering; a linked list 链表 lets data grow and shrink.

ADTs can be built from other ADTs, not just from arrays: a queue from two stacks; a stack from a linked list (push = prepend a head node 节点); a queue from a linked list with head and tail pointers 指针; a binary tree 二叉树 from nodes with two child pointers; a dictionary 字典 stores key→value pairs (often on a hash table). Layering this way separates concerns — the algorithm using the ADT need not know how it is built.

The ADTs the exam asks you to describe and implement

Stack (last in, first out): items are added (pushed) and removed (popped) at the same end, the top; a pointer TopOfStack holds the index of the top item. Implemented with an array and that one pointer: push checks the stack is not full, increments the pointer and stores the item; pop checks it is not empty, returns the top item and decrements the pointer.

FUNCTION Push(Item : INTEGER) RETURNS BOOLEAN
    IF TopOfStack = 9 THEN      // full (array 0 to 9)
        RETURN FALSE
    ENDIF
    TopOfStack ← TopOfStack + 1
    StackData[TopOfStack] ← Item
    RETURN TRUE
ENDFUNCTION
FUNCTION Pop() RETURNS INTEGER
    IF TopOfStack = -1 THEN      // empty
        RETURN -1
    ENDIF
    TopOfStack ← TopOfStack - 1
    RETURN StackData[TopOfStack + 1]
ENDFUNCTION

Queue (first in, first out): items join at the rear (enqueue) and leave from the front (dequeue); two pointers and a count. In a linear queue the front pointer creeps along the array until the space at the start is wasted; a circular queue 循环队列 wraps both pointers round with MOD, so every cell is reused.

A circular queue of six array cells holding three items in cells 3 to 5, with the front pointer at 3 and the rear at 5, and a dashed arrow showing that the next item wraps round into cell 0
A circular queue: the rear and front pointers step forward with MOD, so the array's first cells are reused once their items have left
FUNCTION Enqueue(Item : STRING) RETURNS BOOLEAN
    IF Count = 6 THEN      // full
        RETURN FALSE
    ENDIF
    Rear ← (Rear + 1) MOD 6
    QueueArray[Rear] ← Item
    Count ← Count + 1
    RETURN TRUE
ENDFUNCTION
FUNCTION Dequeue() RETURNS STRING
    IF Count = 0 THEN      // empty
        RETURN ""
    ENDIF
    DECLARE Item : STRING
    Item ← QueueArray[Front]
    Front ← (Front + 1) MOD 6
    Count ← Count - 1
    RETURN Item
ENDFUNCTION

Linked list: a sequence of nodes, each holding a data item and a pointer to the next node; a start pointer gives the first node and a null pointer (0 or $-1$) ends the list. In an array implementation two parallel arrays hold the data and the pointers, and unused cells are chained into a free list 空闲列表 so that an insertion knows where to put the new node.

Two parallel arrays Data and Pointer implementing a linked list of the names Ann, Ben and Dan: the start pointer is 1, the pointers chain 1 to 3 to 2 to 0, and the unused cells 4, 5 and 6 form the free list
A linked list in two arrays: the order of the list is in the pointers, not in the positions; inserting a name means taking a cell from the free list and re-linking two pointers
FUNCTION FindInList(Target : STRING) RETURNS INTEGER   // index, or 0 if absent
    DECLARE Current : INTEGER
    Current ← Start
    WHILE Current <> 0
        IF Data[Current] = Target THEN
            RETURN Current
        ENDIF
        Current ← Pointer[Current]
    ENDWHILE
    RETURN 0
ENDFUNCTION

To insert into an ordered list: take the first free cell (NewNode ← FreeList, FreeList ← Pointer[FreeList]), store the item, then walk the list with a Previous and Current pointer until Data[Current] > Item or the end; set Pointer[NewNode] ← Current and Pointer[Previous] ← NewNode (or Start ← NewNode if it goes first). To delete, re-link the previous node past the deleted one and return the cell to the free list.

Binary tree: a root node, each node holding data, a left pointer to a subtree of smaller values and a right pointer to a subtree of larger values. Implemented as a 2D array (or three 1D arrays) Tree[Index, 0..2] for left pointer, data, right pointer, with a root pointer and a next-free pointer.

FUNCTION FindInTree(Target : INTEGER) RETURNS INTEGER   // index, or -1
    DECLARE Current : INTEGER
    Current ← Root
    WHILE Current <> -1
        IF Tree[Current, 1] = Target THEN
            RETURN Current
        ENDIF
        IF Target < Tree[Current, 1] THEN
            Current ← Tree[Current, 0]      // go left
        ELSE
            Current ← Tree[Current, 2]      // go right
        ENDIF
    ENDWHILE
    RETURN -1
ENDFUNCTION

To insert: store the item in the next free node with both pointers $-1$; if the tree is empty make it the root; otherwise walk down from the root, going left or right by comparison, until the pointer you would follow is $-1$, and set that pointer to the new node. An ADT from another ADT: a stack is a linked list where push and pop both work at the start; a queue is a linked list with a start and an end pointer; a queue can be made from two stacks (push onto one, pop from the other, moving everything across when the second is empty); a binary tree's nodes are records or objects linked by pointers, so it is built from a linked structure of nodes. Say which operations of the new ADT map onto which operations of the old one.

The first figure shows a binary search tree: each node has up to two child nodes, values smaller than a node go to its left, and larger values go to its right. The second figure shows the three ways to traverse a tree.

A binary search tree with root 27, a left subtree of 19, 16, 21 and 17, and a right subtree of 36, 30, 55 and 89, with the root, the left and right pointers, and a leaf node labelled
A binary search tree: each node has up to two child nodes
A binary search tree with root 4 (left subtree 2 over 1 and 3, right subtree 6 over 5 and 7); pre-order visits 4 2 1 3 6 5 7, in-order 1 2 3 4 5 6 7 (sorted), post-order 1 3 2 5 7 6 4
Three depth-first traversals of a binary tree: pre-order, in-order (sorted order) and post-order
词汇 训练
English 中文 拼音
linked list/lɪŋkt lɪst/ 链表 liàn biǎo
stack/stæk/ 栈 zhàn
queue/kjuː/ 队列 duì liè
node/nəʊd/ 节点 jié diǎn
pointers/ˈpɔɪntəz/ 指针 zhǐ zhēn
binary tree/ˈbaɪnəri triː/ 二叉树 èr chā shù
dictionary/ˈdɪkʃənəri/ 字典 zì diǎn
circular queue/ˈsɜːkjʊlə kjuː/ 循环队列 xún huán duì liè
free list/friː lɪst/ 空闲列表 kòng xián liè biǎo
19.1

比较算法

English

Time complexity

Time complexity 时间复杂度 is how the running time grows with input size $n$, written in Big-O notation 大O表示法 (the dominant term): O(1) constant, O($\log n$) binary search, O($n$) linear search, O($n \log n$) good sorts, O($n^{2}$) bubble/insertion sort. A smaller order is better at scale, even if another algorithm is faster for small $n$.

To make that concrete: to sort a million items, an $O(n \log n)$ sort finishes in a fraction of a second, while an $O(n^{2})$ sort can take minutes.

Worked example. A sorted list holds $1000$ items. How many comparisons does each search need in the worst case?

A linear search checks items one at a time, so it may need up to $1000$ comparisons — this is $O(n)$. A binary search halves the list each step, so it needs at most $\lceil \log_2 1000 \rceil = 10$ comparisons — this is $O(\log n)$. Doubling the list to $2000$ items adds only one comparison to the binary search, but up to another $1000$ to the linear search — which is why the order of growth, not raw speed, decides the winner at scale.

Describing an order. O(1): the time is constant, independent of the number of items (pushing onto a stack, reading an array element). O($\log n$): the time grows with the logarithm of the number of items, so doubling the data adds only a fixed extra step (binary search). O($n$): the time grows in proportion to the number of items (linear search, one pass through a list). O($n \log n$): a little worse than linear (efficient sorts). O($n^{2}$): the time grows with the square of the number of items, so doubling the data quadruples the time (bubble and insertion sort). “State the Big O of a binary search of Names[0:99]” is answered $O(\log n)$, and “describe its meaning” as above; Big O measures how the time or memory scales, not the actual time.

Worked example (the order of a piece of code). State the time complexity of each fragment, where n is the number of items.

// Fragment A
FOR i ← 1 TO n
    FOR j ← 1 TO n
        Total ← Total + Grid[i, j]
    NEXT j
NEXT i

// Fragment B
Count ← 0
WHILE n > 1
    n ← n DIV 2
    Count ← Count + 1
ENDWHILE

// Fragment C
FOR i ← 1 TO n
    Total ← Total + A[i]
NEXT i
FOR i ← 1 TO n
    OUTPUT A[i] / Total
NEXT i

A is $O(n^{2})$: the inner statement runs $n$ times for each of the $n$ outer passes, $n \times n$ in all. B is $O(\log n)$: $n$ halves each pass, so the loop runs about $\log_2 n$ times (for $n = 64$, six passes). C is $O(n)$: two loops one after the other take $n + n = 2n$ steps, and constants are dropped. The rule: nested loops multiply, loops in sequence add, and only the fastest-growing term is kept, so $3n^{2} + 5n + 2$ is $O(n^{2})$.

A graph of running time against input size n for the common orders: O(1) and O(log n) stay almost flat, O(n) rises gently, O(n log n) more steeply, and O(n squared) climbs away fastest
How the common orders of growth compare: a smaller order wins at scale
A line graph of running time against the number of elements n: bubble sort and insertion sort rise steeply as O(n squared), while quick sort stays low as O(n log n)
How sorting time grows with the number of elements $n$: $O(n^2)$ sorts climb away from an $O(n\log n)$ sort

Space complexity

Space complexity 空间复杂度 is the extra memory needed. Bubble and insertion sort use O(1) extra (in place); merge sort uses O($n$); recursion uses stack memory proportional to its depth. There is often a time–memory trade-off.

Other criteria

Simplicity (easier to code and maintain), stability, and adaptiveness (faster on nearly-sorted data). The right algorithm depends on the data and the constraints.

探索

运行时间如何随 n 增长

把 n 向上滑动并比较曲线:O(1) 和 O(log n) 几乎保持平坦,O(n) 稳步上升,O(n²) 爆炸。这就是为什么大 O——而不是一个秒表——是我们在大输入上比较算法的方式。

探索

大 O 增长

改变输入大小 n,比较每种算法工作量增长的快慢——时间复杂度背后的概念。

词汇 训练
English 中文 拼音
time complexity/taɪm kəmˈpleksɪti/ 时间复杂度 shí jiān fù zá dù
Big-O notation/bɪɡ əʊ nəʊˈteɪʃn/ 大O表示法 dà O biǎo shì fǎ
space complexity/speɪs kəmˈpleksɪti/ 空间复杂度 kōng jiān fù zá dù
19.2

递归

教学大纲
考生应具备的能力 注释与指导
展示对递归的理解 递归的关键特征;递归在编程语言中的表达方式;编写并追踪递归算法;使用递归的优势场景
展示对编译器翻译递归代码所需工作的认知 栈的使用与**回溯(unwinding)**机制

来源:Cambridge International 教学大纲

English
Recursion: the call stack winds up and unwinds
Four call boxes, each indented below the last: total([4, 7, 2]) = 4 + total([7, 2]); total([7, 2]) = 7 + total([2]); total([2]) = 2 + total([]); total([]) = 0 is the base case. Values return upward: 2, then 9, then 13
Calls go down to the base case; results come back up

Recursive algorithms use recursion 递归: the routine calls itself with a smaller version of the same problem, until a base case 基本情形 ends the chain. It has two parts: the base case (small enough to solve directly — without it the recursion never stops) and the recursive case 递归情形 (reduce the input and call itself).

Factorial 阶乘:

FUNCTION Factorial(n : INTEGER) RETURNS INTEGER
    IF n = 0 OR n = 1 THEN
        RETURN 1
    ELSE
        RETURN n * Factorial(n - 1)
    ENDIF
ENDFUNCTION

Recursion is natural for self-similar problems: trees, divide-and-conquer 分治 (binary search, merge sort), and nested data. When it is a poor fit, a loop is usually cleaner.

“Describe what is meant by recursion” (two marks). A function or procedure that is defined in terms of itself: it calls itself from within its own body, with a smaller version of the problem each time, until a base case is reached. “State three essential features of recursion”: (1) a base case (stopping condition) that returns a value without a further call; (2) a general case 一般情形 in which the routine calls itself; (3) each call moves the problem closer to the base case (the parameter is reduced), so that the recursion terminates. Some schemes add: values are returned as the calls unwind.

“Describe when the use of recursion is beneficial, and give an example.” When the problem is naturally defined in terms of smaller versions of itself, so that the recursive solution is shorter, clearer and closer to the mathematical definition than a loop would be: a factorial or Fibonacci number, a binary search, traversing a binary tree, merge sort or quicksort, and processing nested structures such as folders within folders. It is a poor choice when the depth is large (the stack may overflow) or when the same sub-problem is computed many times (naive Fibonacci).

Tracing a recursive call

For Factorial(4): the calls go down to Factorial(1)=1, then unwinding multiplies back up: 2*1=2, 3*2=6, 4*6=24. Final result 24. Track each pending call on a stack.

Worked example. The function below is given without an explanation. Trace Unknown(3, 5) and state its output and return value.

FUNCTION Unknown(BYVAL X, BYVAL Y : INTEGER) RETURNS INTEGER
    IF X < Y THEN
        OUTPUT X + Y
        RETURN Unknown(X + 1, Y - 1) + 1
    ELSE
        RETURN 0
    ENDIF
ENDFUNCTION

Call 1: $X = 3, Y = 5$: $3 < 5$, output 8, call Unknown(4, 4). Call 2: $4 < 4$ is false, return 0. Unwinding: call 1 returns $0 + 1 = 1$. Output 8, return value 1. Write the trace as a table with a row per call (parameters, condition, output, what it returns), and do the returns from the deepest call upwards: that is the unwinding the mark scheme looks for.

Worked example (Fibonacci). Fib(n) returns n when n < 2, otherwise Fib(n - 1) + Fib(n - 2). Find Fib(5).

Fib(5) = Fib(4) + Fib(3); Fib(4) = Fib(3) + Fib(2); Fib(3) = Fib(2) + Fib(1); Fib(2) = Fib(1) + Fib(0) = 1 + 0 = 1. So Fib(3) = 1 + 1 = 2, Fib(4) = 2 + 1 = 3, Fib(5) = 3 + 2 = 5. The base case is reached many times (Fib(2) is computed three times), which is why this version is slow: it makes 15 calls for $n = 5$ and roughly doubles the calls for every increase in $n$.

Converting recursion to iteration. A recursive routine can be rewritten iteratively; some need an explicit stack to hold the state of pending calls. Factorial as a loop:

FUNCTION Factorial(N : INTEGER) RETURNS INTEGER
    DECLARE Result, Count : INTEGER
    Result ← 1
    FOR Count ← 2 TO N
        Result ← Result * Count
    NEXT Count
    RETURN Result
ENDFUNCTION

Asked to change a recursive insertion sort or search into an iterative one, replace the self-call with a loop over the index that the recursion was stepping through, and turn the base case into the loop's exit condition.

The call stack for Factorial(4): each call pushes a frame down to the base case Factorial(1)=1, then the stack unwinds, returning 2 = 2 times 1, 6 = 3 times 2 and 24 = 4 times 6
Recursion uses the call stack: calls push frames down to the base case, then returns unwind back up

Risks

  • infinite recursion if the base case is missed — crashes with a stack overflow 栈溢出.
  • high memory use for deep recursion.
  • slow if it repeats work (naive Fibonacci is exponential — use a loop or memoisation 记忆化).
探索

递归从叶子向上展开

按调用实际完成的顺序逐步走过 fib(4):叶子(基本情形)先解决,然后每个父节点合并它的子节点。注意 fib(2) 被计算两次——那重复的工作就是为什么朴素的递归慢。

词汇 训练
English 中文 拼音
recursion/rɪˈkɜːʃn/ 递归 dì guī
base case/beɪs keɪs/ 基本情形 jī běn qíng xíng
recursive case/rɪˈkɜːsɪv keɪs/ 递归情形 dì guī qíng xíng
factorial/fækˈtɔːrɪəl/ 阶乘 jiē chéng
divide-and-conquer/dɪˈvaɪd ænd ˈkɒŋkə/ 分治 fēn zhì
general case/ˈdʒenərəl keɪs/ 一般情形 yì bān qíng xíng
stack overflow/stæk ˌəʊvəˈfləʊ/ 栈溢出 zhàn yì chū
memoisation/ˌmeməʊaɪˈzeɪʃn/ 记忆化 jì yì huà
19.2

编译器如何处理递归代码

English

Recursion needs each call to have its own copy of its parameters 参数 and local variables 局部变量. The compiler keeps these on the call stack 调用栈. For each call it pushes a stack frame 栈帧 holding the parameters, the local variables, and the return address 返回地址 (where to resume in the caller). When the function returns, the return value is handed back, the frame is popped, and control resumes at the return address.

The call stack for factorial(3): each call waits, then returns in reverse order
The call stack for factorial(3): each call waits, then returns in reverse order

Because each call has its own frame, recursive calls don't trample each other's variables. The stack can grow large for deep recursion, which is why very deep recursion may overflow it. This is the same call-and-return mechanism used for ordinary (non-recursive) calls — there is no special “recursion mechanism”.

“Explain why a stack is suitable for implementing recursion” (three marks). Each recursive call must save its return address, its parameters and its local variables, and the calls are completed in the reverse order to that in which they were made (the last call made is the first to finish), which is exactly the last in, first out behaviour of a stack: each new call pushes a frame, and each return pops the most recent frame, restoring the caller's state and telling it where to continue. This is the compiler's job when it translates recursive code: it generates the push of a stack frame on every call and the pop on every return, and the frames are unwound as the results come back.

词汇 训练
English 中文 拼音
call stack/kɔːl stæk/ 调用栈 diào yòng zhàn
parameters/pəˈræmɪtəz/ 参数 cān shù
local variables/ˈləʊkl ˈveərɪəblz/ 局部变量 jú bù biàn liàng
stack frame/stæk freɪm/ 栈帧 zhàn zhēn
return address/rɪˈtɜːn əˈdres/ 返回地址 fǎn huí dì zhǐ
19.2

考官认可的定义

English

A definition question is marked against fixed wording. Learn these exactly, and give one answer only.

Term Definition
linear search checking each item in turn from the start until the target is found or the end is reached
binary search repeatedly comparing the target with the middle item of a sorted list and discarding the half that cannot contain it
bubble sort repeatedly passing through the list, swapping adjacent items that are in the wrong order, until a pass makes no swaps
insertion sort taking each item in turn and inserting it into its correct place among the items already sorted
abstract data type a collection of data and the operations that can be performed on it, defined independently of how it is stored
stack a last-in-first-out structure with push and pop at the top
queue a first-in-first-out structure with items added at the rear and removed from the front
linked list a sequence of nodes, each holding data and a pointer to the next node, with a start pointer
binary tree nodes each holding data and pointers to a left subtree of smaller values and a right subtree of larger values
Big O notation a way of classifying the time (or memory) an algorithm needs by how it grows with the size of the input
recursion a routine that calls itself with a smaller version of the problem until a base case stops the calls
base case the condition under which a recursive routine returns without calling itself
unwinding the returns of a chain of recursive calls, from the deepest call back to the first, as the stack frames are popped
19.2

考试技巧

English
  • Searches: linear needs no order and O($n$); binary needs a sorted array, halves each time and is O($\log n$). Know both algorithms by heart, including the bounds and the flag.
  • Sorts: bubble with a swapped flag, insertion with a key that shifts larger items right; both O($n^{2}$) worst, O($n$) on sorted data. Performance depends on the number of items and how ordered they are.
  • ADT implementations are pointer bookkeeping: a top pointer; front, rear and count with MOD; start, pointers and a free list; root with left and right pointers. Always check for full and empty.
  • Big O is about scaling: constant, logarithmic, linear, square. Say “doubling the data adds one comparison” for a binary search.
  • Recursion: base case, general case, progress towards the base case; beneficial when the problem is defined in terms of itself; a stack holds the return addresses and variables because calls return in reverse order. Trace with a table and unwind from the deepest call.

Common mistakes

  • Writing $O(2n)$ or $O(n^{2} + n)$: drop constants and smaller terms, giving $O(n)$ and $O(n^{2})$.
  • Computing the middle index as $(Lower + Upper) / 2$ in a trace: the exam's version uses DIV, so round down.
  • Using a binary search on unsorted data, or on a linked list; and setting Lower ← MiddleIndex instead of MiddleIndex + 1, which loops for ever.
  • A bubble sort inner loop that runs to the end of the array every pass, or a swap without a temporary variable.
  • A push or enqueue that does not test for full, or a pop or dequeue that does not test for empty.
  • Moving the queue's front pointer without MOD in a circular queue, or treating front = rear as always meaning empty.
  • Inserting into a linked list by shifting the array contents; only the pointers change.
  • A recursive function with no base case, or one whose recursive call does not make the problem smaller.
  • Tracing a recursive call but forgetting to add the pending work on the way back up.
  • Answering “why a stack” with “because it is fast”; the reason is the last-in-first-out order of the returns.

该知识点的互动课程

逐步完成,配合即时检查练习。

历年真题

更多 A-Level 计算机科学 知识点

IGCSE, A-Level & AP