📚 Algorithm Essentials for IGCSE CIE Computer Science | IGCSE CIE 计算机:算法 考点精讲
Algorithms form the foundation of all computer programs. In IGCSE CIE Computer Science (0478/0984), you need to design, express, and evaluate algorithms using pseudocode and flowcharts. This guide covers every core concept: from basic building blocks to standard search and sort routines, along with testing strategies and structured techniques.
算法是所有计算机程序的基础。在 IGCSE CIE 计算机科学(0478/0984)中,你需要学会用伪代码和流程图设计、表达和评估算法。本指南涵盖所有核心概念:从基本构件到标准搜索与排序程序,再到测试策略与结构化技术。
1. What is an Algorithm? | 什么是算法?
An algorithm is a step-by-step procedure to solve a problem or perform a task. It must be unambiguous, finite, and effective. Each step should be clearly defined so that a machine or another person can follow it without guessing.
算法是解决一个问题或执行一项任务的逐步过程。它必须明确、有限且有效。每一步都应当清晰定义,以便机器或其他人能无需猜测即可执行。
In computer science, algorithms receive inputs, process data using sequence, selection, and iteration, then produce outputs. Good algorithms are correct, efficient, and easy to understand.
在计算机科学中,算法接收输入,利用顺序、选择和迭代处理数据,然后产生输出。好的算法应当正确、高效且易于理解。
2. Pseudocode and Flowcharts | 伪代码与流程图
Algorithms can be expressed in pseudocode or as flowcharts. Pseudocode uses structured English-like statements with consistent indentation and keywords. The CIE syllabus expects you to read and write pseudocode using specific conventions, such as INPUT, OUTPUT, IF…THEN…ELSE…ENDIF, and WHILE…DO…ENDWHILE.
算法可以用伪代码或流程图来表达。伪代码使用类似于英语的结构化语句,具有一致的缩进和关键字。CIE 大纲要求你按照特定规范阅读和编写伪代码,例如 INPUT、OUTPUT、IF…THEN…ELSE…ENDIF 和 WHILE…DO…ENDWHILE。
Flowcharts use standard symbols: ovals for start/stop, parallelograms for input/output, rectangles for processes, diamonds for decisions, and arrows to show flow. Both tools help visualise logic before coding.
流程图使用标准符号:椭圆表示开始/结束,平行四边形表示输入/输出,矩形表示处理,菱形表示决策,箭头表示控制流。这两种工具都有助于在编码前将逻辑可视化。
3. Variables and Data Types | 变量与数据类型
Variables are named storage locations that hold values. In pseudocode, a variable is created by assigning a value to it, for example: Score ← 85. The value can change during execution.
变量是命名的存储位置,用于保存值。在伪代码中,通过赋值来创建变量,例如:Score ← 85。变量的值可以在执行过程中改变。
Common data types include INTEGER, REAL, CHAR, STRING, and BOOLEAN. Every variable should have a clear purpose, and its type must suit the data it stores. Good variable names describe their content, like TotalMark or IsValid.
常见数据类型包括 INTEGER、REAL、CHAR、STRING 和 BOOLEAN。每个变量都应有明确的目的,其类型必须适合所存数据。好的变量名能描述其内容,如 TotalMark 或 IsValid。
4. Input and Output | 输入与输出
An algorithm must interact with the user or its environment. In CIE pseudocode, INPUT reads a value from a keyboard or sensor, and OUTPUT sends a result to the screen or another device.
算法必须与用户或环境交互。在 CIE 伪代码中,INPUT 从键盘或传感器读取值,OUTPUT 将结果发送到屏幕或其他设备。
Example: INPUT Name
OUTPUT "Hello ", Name
Input stores the characters typed by the user into the variable. Output can display literals, variable values, or expressions.
示例:INPUT Name
OUTPUT "Hello ", Name
INPUT 将用户输入的字符存入变量。OUTPUT 可以显示字面量、变量值或表达式。
5. Sequence, Selection, Iteration | 顺序、选择与迭代
Every algorithm is built from three basic control structures: sequence (statements executed one after another), selection (a decision that chooses between branches), and iteration (repetition of a block of code).
每个算法都由三种基本控制结构构建而成:顺序(语句一条接一条执行)、选择(在分支之间做出决策)和迭代(重复执行一段代码)。
Sequence is the default flow. Selection includes IF statements and CASE constructs. Iteration comes in three forms: definite loops (FOR…NEXT), pre-condition loops (WHILE…DO…ENDWHILE), and post-condition loops (REPEAT…UNTIL).
顺序是默认流程。选择包括 IF 语句和 CASE 结构。迭代有三种形式:计数循环(FOR…NEXT)、前测循环(WHILE…DO…ENDWHILE)和后测循环(REPEAT…UNTIL)。
6. IF…THEN…ELSE…ENDIF | 条件判断
The IF statement lets an algorithm make a decision. The basic form is IF condition THEN … ENDIF. For two-way branching, add ELSE. Conditions use relational operators like <, >, =, <=, >=, <>, and logical operators AND, OR, NOT.
IF 语句让算法能做决策。基本形式是 IF 条件 THEN … ENDIF。对于两路分支,添加 ELSE。条件使用关系运算符(如 <、>、=、<=、>=、<>)和逻辑运算符 AND、OR、NOT。
CIE pseudocode example:IF Age >= 18
THEN
OUTPUT "Adult"
ELSE
OUTPUT "Minor"
ENDIF
Always use ENDIF to close the block.
CIE 伪代码示例:IF Age >= 18
THEN
OUTPUT "Adult"
ELSE
OUTPUT "Minor"
ENDIF
务必使用 ENDIF 来闭合代码块。
7. CASE…OF…OTHERWISE…ENDCASE | 多分支选择
When there are many possible values for a single variable, a CASE statement is cleaner than multiple IFs. The structure is CASE OF label : statement : label : statement … OTHERWISE statement ENDCASE.
当一个变量有多个可能取值时,CASE 语句比多层 IF 更简洁。其结构为 CASE OF 标签 : 语句 : 标签 : 语句 … OTHERWISE 语句 ENDCASE。
Example:CASE OF Grade
'A' : OUTPUT "Excellent"
'B' : OUTPUT "Good"
'C' : OUTPUT "Satisfactory"
OTHERWISE OUTPUT "Fail"
ENDCASE
OTHERWISE handles any case not explicitly listed, similar to ELSE in an IF.
示例:CASE OF Grade
'A' : OUTPUT "Excellent"
'B' : OUTPUT "Good"
'C' : OUTPUT "Satisfactory"
OTHERWISE OUTPUT "Fail"
ENDCASE
OTHERWISE 处理未明确列出的情况,类似于 IF 中的 ELSE。
8. FOR, WHILE, REPEAT Loops | 计数循环、前测循环和后测循环
FOR…TO…NEXT is used when the number of repetitions is known in advance. Syntax: FOR Identifier ← start TO finish [STEP increment]. The loop runs a fixed number of times.
FOR…TO…NEXT 用于事先知道重复次数的情况。语法:FOR 标识符 ← 起始值 TO 终止值 [STEP 步长]。循环运行固定次数。
WHILE…DO…ENDWHILE tests the condition at the beginning. If the condition is false initially, the body never executes. This is a pre-condition loop.
WHILE…DO…ENDWHILE 在开始时测试条件。如果条件初始为假,循环体从不执行。这是一种前测循环。
REPEAT…UNTIL tests the condition at the end, so the body always runs at least once. This is a post-condition loop. CIE pseudocode uses REPEAT…UNTIL exactly as shown.
REPEAT…UNTIL 在结束时测试条件,因此循环体至少执行一次。这是一种后测循环。CIE 伪代码中使用 REPEAT…UNTIL 格式。
9. Standard Algorithms: Totalling and Counting | 标准算法:求和与计数
Totalling means accumulating a sum: initialise a variable to zero, then add each new value inside a loop. For example:Total ← 0
FOR i ← 1 TO 10
INPUT Num
Total ← Total + Num
NEXT i
求和意味着累加总和:将变量初始化为零,然后在循环中加上每个新值。例如:Total ← 0
FOR i ← 1 TO 10
INPUT Num
Total ← Total + Num
NEXT i
Counting keeps track of how many items meet a condition. Set a counter to zero, and increment it whenever the condition is true:
计数用于记录有多少项满足某一条件。将计数器设为零,每当条件为真时自增:
Count ← 0
FOR i ← 1 TO 10
INPUT Num
IF Num > 50 THEN Count ← Count + 1
NEXT i
OUTPUT Count
These patterns repeat throughout the syllabus and are the basis for average, maximum, and minimum algorithms.
这些模式贯穿整个大纲,是求平均值、最大值和最小值算法的基础。
10. Search Algorithms: Linear and Binary | 搜索算法:线性搜索与二分搜索
Linear search examines each element in turn until the target is found or the list ends. It works on unsorted data. CIE pseudocode often uses a WHILE loop with a flag or an index.
线性搜索依次检查每个元素,直到找到目标或遍历完列表。它适用于无序数据。CIE 伪代码常使用 WHILE 循环配合标志或索引。
Example:Found ← FALSE
Index ← 1
WHILE Found = FALSE AND Index <= Length
IF Array[Index] = Target THEN Found ← TRUE
ELSE Index ← Index + 1
ENDWHILE
Binary search requires a sorted array. It repeatedly divides the search interval in half: compare the middle element with the target, then discard the half that cannot contain it. Binary search is far faster on large lists but only works on sorted data.
二分搜索要求数组有序。它反复将搜索区间一分为二:将中间元素与目标比较,然后丢弃不可能包含目标的那一半。二分搜索在大型列表中要快得多,但仅适用于有序数据。
11. Bubble Sort Algorithm | 冒泡排序算法
Bubble sort is the only sorting algorithm required for CIE IGCSE. It compares adjacent elements and swaps them if they are in the wrong order. This pass is repeated until no swaps are needed.
冒泡排序是 CIE IGCSE 要求掌握的唯一排序算法。它比较相邻元素,如果顺序错误则交换。重复这样的遍历,直到不再需要交换。
The algorithm uses two nested loops: an outer loop to control the number of passes, and an inner loop to compare and swap. An optimisation uses a “swapped” flag to stop early if the list becomes sorted before the maximum passes.
该算法使用两层嵌套循环:外层循环控制遍历次数,内层循环进行比较和交换。一种优化是使用“swapped”标志,如果列表提前有序,则提前终止。
CIE-style pseudocode:REPEAT
Swapped ← FALSE
FOR i ← 1 TO Length - 1
IF List[i] > List[i+1]
THEN
Temp ← List[i]
List[i] ← List[i+1]
List[i+1] ← Temp
Swapped ← TRUE
ENDIF
NEXT i
UNTIL Swapped = FALSE
12. Testing and Test Data | 测试与测试数据
Testing ensures an algorithm works correctly. You need to consider normal data (expected values), abnormal/erroneous data (values that should be rejected or cause an error message), and boundary data (values at the limits of acceptance).
测试确保算法正确运行。你需要考虑正常数据(预期值)、异常/错误数据(应被拒绝或引发错误消息的值)和边界数据(接受范围极限处的值)。
For example, a grade algorithm accepting marks 0–100 should be tested with 50 (normal), -5 and 105 (abnormal), and 0 and 100 (boundary). A test plan lists input, expected output, and actual result. Dry-run tracing with a trace table helps identify logic errors.
例如,一个接受 0–100 分数的成绩算法应用 50(正常)、-5 和 105(异常)、0 和 100(边界)进行测试。测试计划列出输入、预期输出和实际结果。使用跟踪表的干运行有助于找出逻辑错误。
Always check that loops terminate under all conditions, that CASE statements handle every option, and that variables are initialised before use. Thorough testing is a key exam requirement.
始终检查循环在所有条件下是否终止,CASE 语句是否处理每种选项,变量使用前是否初始化。全面测试是考试中的关键要求。
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课程辅导,国外大学本科硕士研究生博士课程论文辅导