📚 IGCSE OCR Computer Science: Algorithms Key Points | IGCSE OCR 计算机:算法 考点精讲
Algorithms form the very heart of computer science. In the IGCSE OCR specification, you are expected not only to understand what an algorithm is, but also to design, represent, and evaluate algorithms using pseudocode and flowcharts. This article breaks down every essential topic you need to master — from basic constructs to searching and sorting — with paired explanations in English and Chinese to reinforce your understanding.
算法是计算机科学的核心。在 IGCSE OCR 大纲中,你不仅要理解算法的概念,还要能够使用伪代码和流程图来设计、表示和评估算法。本文将拆解你所需掌握的每个重要主题——从基本结构到搜索与排序——并以中英对照的方式解释,帮助你加深理解。
1. What is an Algorithm? | 什么是算法?
An algorithm is a step-by-step procedure for solving a problem or completing a task. It consists of a finite sequence of well-defined instructions that, when executed in the correct order, produce a desired result. Algorithms must be unambiguous, have a clear start and end, and be capable of handling a range of inputs.
算法是解决问题或完成任务的一步一步的过程。它由一系列定义明确的指令组成,按正确顺序执行后可以产生预期的结果。算法必须无歧义,有清晰的开始和结束,并能处理多种输入。
In computational thinking, the key properties of a good algorithm are correctness, efficiency, and clarity. You must be able to think algorithmically — breaking complex problems into smaller, manageable steps.
在计算思维中,好算法的关键属性是正确性、高效性和清晰性。你必须具备算法思维能力——将复杂问题分解为更小、可管理的步骤。
OCR exam questions often ask you to identify algorithmic logic from a given scenario. Always focus on the essential steps and the order in which they must be performed. Never assume implicit steps; algorithms must be explicit.
OCR 考试题目常常要求你从给定情境中识别算法逻辑。要始终关注关键步骤及其执行顺序。不要假设隐式步骤;算法必须明确。
2. Pseudocode and Flowcharts | 伪代码与流程图
Pseudocode is a simplified, half-code, half-English way of describing algorithms. There is no strict syntax, but the OCR examination uses a specific pseudocode style detailed in the specification. You must become familiar with common keywords like INPUT, OUTPUT, IF...THEN...ELSE...ENDIF, FOR...ENDFOR, WHILE...ENDWHILE, and REPEAT...UNTIL.
伪代码是一种简化、半代码半英语的描述算法的方式。虽然没有严格语法,但 OCR 考试使用规范中详细的特定伪代码风格。你必须熟悉常见关键词,如 INPUT、OUTPUT、IF...THEN...ELSE...ENDIF、FOR...ENDFOR、WHILE...ENDWHILE 和 REPEAT...UNTIL。
Flowcharts are graphical representations of algorithms. The standard symbols you need to know include the oval for Start/End, the rectangle for processes, the parallelogram for Input/Output, and the diamond for decisions. Arrows show the flow of control. When drawing flowcharts by hand, neatness and clarity are essential because marks are awarded for correct logic and symbol usage.
流程图是算法的图形化表示。你需要了解的标准符号包括:椭圆形表示开始/结束,矩形表示处理过程,平行四边形表示输入/输出,菱形表示判断。箭头表示控制流向。手绘流程图时,整洁和清晰至关重要,因为正确逻辑和符号使用都会得分。
You may be asked to convert between pseudocode and a flowchart. Practice identifying the key constructs and mapping them precisely. For example, a diamond in a flowchart corresponds to an IF statement in pseudocode, and a loop back arrow maps to a FOR, WHILE, or REPEAT loop.
你可能会被要求在伪代码和流程图之间进行转换。练习识别关键结构并精确映射它们。例如,流程图中的菱形对应伪代码中的 IF 语句,而回路箭头对应 FOR、WHILE 或 REPEAT 循环。
3. Variables and Constants | 变量与常量
Variables are named storage locations in memory whose values can change while a program runs. Constants are named values that do not change during execution. In pseudocode, you can assign a value with the ← operator, for example: score ← 0 or name ← "Alice".
变量是内存中命名的存储位置,其值在程序运行时可以改变。常量是命名的值,在执行期间不会改变。在伪代码中,你可以使用 ← 运算符赋值,例如:score ← 0 或 name ← "Alice"。
Choosing meaningful variable names (e.g., total_price rather than tp) makes algorithms more readable and reduces errors. OCR expects you to declare variables implicitly through assignment; you do not need separate declaration statements unless the question specifies.
选择有意义的变量名(例如 total_price 而不是 tp)使算法更具可读性并减少错误。OCR 要求你通过赋值隐式声明变量;除非题目指定,否则无需单独的声明语句。
Constants are used for fixed values such as PI ← 3.14 or VAT_RATE ← 0.2. Using constants instead of magic numbers makes algorithms easier to maintain and understand.
常量用于固定值,例如 PI ← 3.14 或 VAT_RATE ← 0.2。使用常量而不是神秘数字可以使算法更易于维护和理解。
4. Input and Output | 输入与输出
Every algorithm that interacts with a user requires input and output operations. Input allows the user to supply data; output displays results. In OCR pseudocode, INPUT reads a value from the user and assigns it to a variable, while OUTPUT displays a message or a variable's value.
每个与用户交互的算法都需要输入和输出操作。输入让用户提供数据;输出显示结果。在 OCR 伪代码中,INPUT 从用户读取一个值并赋给变量,而 OUTPUT 显示一条消息或变量的值。
For example: INPUT "Enter your age: " age reads a number and stores it in age. OUTPUT "You are ", age displays the message and the value. You can also output the result of an expression: OUTPUT "Total: ", price * quantity.
例如:INPUT "Enter your age: " age 读取一个数字并将其存储在 age 中。OUTPUT "You are ", age 显示消息和值。你还可以输出表达式的结果:OUTPUT "Total: ", price * quantity。
In flowcharts, input/output is represented by a parallelogram. Make sure the direction of data flow is clear. When answering exam questions, always check that your algorithm includes all necessary inputs and outputs as specified in the requirements.
在流程图中,输入/输出用平行四边形表示。确保数据流方向清晰。在回答考题时,请始终检查你的算法是否包含要求中指定的所有必要输入和输出。
5. Arithmetic, Comparison, and Logical Operators | 算术、比较与逻辑运算符
You must be confident with standard arithmetic operators: + (addition), - (subtraction), * (multiplication), / (division), ^ (exponentiation), MOD (remainder after division), and DIV (integer division). These are used to perform calculations on numeric data.
你必须熟练掌握标准算术运算符:+(加)、-(减)、*(乘)、/(除)、^(乘方)、MOD(取余)和 DIV(整数除)。它们用于对数值数据进行计算。
Comparison operators produce Boolean results: = (equal to), ≠ or <> (not equal to), < (less than), > (greater than), <= (less than or equal to), >= (greater than or equal to). These are essential in decision-making and loop conditions.
比较运算符产生布尔结果:=(等于)、≠ 或 <>(不等于)、<(小于)、>(大于)、<=(小于或等于)、>=(大于或等于)。它们在决策和循环条件中至关重要。
Logical operators AND, OR, and NOT combine multiple conditions. Use them to implement complex selection criteria. For example, IF score >= 50 AND score <= 100 THEN ... tests whether a score lies within a valid range.
逻辑运算符 AND、OR 和 NOT 用于组合多个条件。使用它们来实现复杂的选择标准。例如,IF score >= 50 AND score <= 100 THEN ... 测试分数是否在有效范围内。
Operator precedence follows standard mathematical rules. Use parentheses to make intended order explicit when there is any ambiguity. In pseudocode, * and / are evaluated before + and -.
运算符优先级遵循标准数学规则。当有歧义时,使用括号明确意图。在伪代码中,* 和 / 在 + 和 - 之前计算。
6. Selection: IF Statements | 选择结构:IF语句
Selection allows a program to choose different paths based on conditions. The fundamental structure in OCR pseudocode is the IF...THEN...ELSE...ENDIF block.
选择结构允许程序根据条件选择不同的路径。OCR 伪代码中的基本结构是 IF...THEN...ELSE...ENDIF 块。
A simple IF statement tests one condition and executes a block only if it is true. An IF-ELSE gives two alternative paths. For multiple conditions, you can use nested IF statements or the ELSE IF construct, though OCR pseudocode often uses nested IFs inside ELSE.
简单的 IF 语句测试一个条件,仅当条件为真时才执行一个块。IF-ELSE 提供两条替代路径。对于多个条件,你可以使用嵌套 IF 语句或 ELSE IF 构造,不过 OCR 伪代码通常使用 ELSE 内的嵌套 IF。
Example:
IF temperature > 30 THEN
OUTPUT "Hot"
ELSE
IF temperature < 10 THEN
OUTPUT "Cold"
ELSE
OUTPUT "Mild"
ENDIF
ENDIF
示例:IF temperature > 30 THEN OUTPUT "Hot" ELSE IF temperature < 10 THEN OUTPUT "Cold" ELSE OUTPUT "Mild" ENDIF ENDIF
In flowcharts, the diamond shape represents the decision. Each branch line must be labeled with the condition outcome (True/False or Yes/No).
在流程图中,菱形表示判断。每个分支线必须标明条件结果(真/假 或 是/否)。
7. Iteration: FOR, WHILE, and REPEAT Loops | 迭代:FOR、WHILE与REPEAT循环
Iteration repeats a block of code. Three loop structures are required by OCR: count-controlled (FOR), pre-condition (WHILE), and post-condition (REPEAT...UNTIL).
迭代重复执行一段代码。OCR 要求三种循环结构:计数控制(FOR)、前条件(WHILE)和后条件(REPEAT...UNTIL)。
A FOR loop runs for a known number of times. Syntax: FOR index ← start TO finish. You can optionally include a STEP value. Example: FOR i ← 1 TO 10 will run 10 times. Use a FOR loop when you know exactly how many iterations are needed.
FOR 循环运行已知次数。语法:FOR index ← start TO finish。你可以选择包含 STEP 值。示例:FOR i ← 1 TO 10 将运行 10 次。当明确知道需要多少次迭代时,使用 FOR 循环。
A WHILE loop checks the condition at the start of each iteration. It may execute zero times if the condition is false initially. A REPEAT...UNTIL loop executes at least once because the condition is tested at the end. Understanding the difference is crucial for selecting the right loop type for a given scenario.
WHILE 循环在每次迭代开始时检查条件。如果条件最初为假,它可能执行零次。REPEAT...UNTIL 循环至少执行一次,因为条件在末尾测试。理解这些区别对于为给定场景选择合适的循环类型至关重要。
You must be able to trace loop execution manually in dry-run exercises. Keep a trace table to track variable values throughout iterations. Many exam questions test your ability to identify loop errors, such as off-by-one mistakes or infinite loops.
你必须能够在徒手追踪练习中手动追踪循环执行过程。保持追踪表来记录每次迭代的变量值。许多考题测试你识别循环错误的能力,例如差一错误或无限循环。
8. String Handling and Arrays | 字符串处理与数组
Strings are sequences of characters. In OCR pseudocode, you can assign string literals using quotes, e.g., name ← "Ada". Concatenation joins strings using +. You can also extract substrings and find lengths using functions like LEN(str), LEFT(str, n), RIGHT(str, n), and MID(str, start, length).
字符串是字符序列。在 OCR 伪代码中,你可以使用引号赋字符串字面量,例如 name ← "Ada"。拼接使用 + 连接字符串。你还可以使用函数如 LEN(str)、LEFT(str, n)、RIGHT(str, n) 和 MID(str, start, length) 来提取子串和查找长度。
Arrays allow you to store multiple values under a single identifier. An array is declared by assigning a list: scores ← [12, 15, 9, 20]. Elements are accessed via zero-based or one-based indexing, depending on the question context. Typically, OCR pseudocode uses scores[0] for the first element, but watch the question instructions carefully.
数组允许你在单个标识符下存储多个值。通过分配一个列表来声明数组:scores ← [12, 15, 9, 20]。元素通过基于零或基于一的索引访问,取决于题目上下文。通常,OCR 伪代码使用 scores[0] 表示第一个元素,但要仔细阅读题目说明。
2D arrays can be declared as lists of lists: grid ← [[1,2],[3,4]]. You need to know how to iterate through arrays using FOR loops, often nesting loops for 2D structures. Trace tables involving array updates are common in exams.
二维数组可以声明为列表的列表:grid ← [[1,2],[3,4]]。你需要知道如何使用 FOR 循环遍历数组,对于二维结构经常要嵌套循环。涉及数组更新的追踪表在考试中很常见。
9. Subroutines: Procedures and Functions | 子程序:过程与函数
Subroutines are named blocks of code that can be called multiple times. There are two types: procedures and functions. A procedure performs a task but does not return a value. A function performs a task and returns a single value though, in practice, functions can return multiple values in some exam questions; read the specification for exact rules.
子程序是命名的代码块,可以被多次调用。有两种类型:过程和函数。过程执行任务但不返回值。函数执行任务并返回一个值,尽管实际上在某些考题中函数可以返回多个值;请阅读规范中的确切规则。
Procedures are defined using PROCEDURE and called by name with parameters if needed. For example: PROCEDURE greet(name) OUTPUT "Hello ", name ENDPROCEDURE. Functions are defined using FUNCTION and return a value using the RETURN keyword.
过程使用 PROCEDURE 定义,通过名称调用,必要时带参数。例如:PROCEDURE greet(name) OUTPUT "Hello ", name ENDPROCEDURE。函数使用 FUNCTION 定义,并使用 RETURN 关键字返回值。
Parameters allow data to be passed into a subroutine. When you call a subroutine, the arguments are matched to the parameters. Subroutines improve modularity, readability, and reusability. Be prepared to write your own subroutines or to complete partially given ones in an exam.
参数允许数据传入子程序。当你调用子程序时,实参会匹配形参。子程序提高了模块化、可读性和可重用性。准备好在考试中编写自己的子程序或补全部分给出的子程序。
10. Standard Searching Algorithms | 标准搜索算法
You need to know two searching algorithms in detail: linear search and binary search. A linear search inspects each element in turn until a match is found or the end of the list is reached. Its time complexity is O(n). It works on unsorted data and is simple to implement.
你需要详细了解两种搜索算法:线性搜索和二分搜索。线性搜索逐个检查每个元素,直到找到匹配项或到达列表末尾。它的时间复杂度为 O(n)。它适用于未排序的数据,实现简单。
Pseudocode for linear search:
FUNCTION linearSearch(array, target)
FOR i ← 0 TO LEN(array) - 1
IF array[i] = target THEN
RETURN i
ENDIF
ENDFOR
RETURN -1
ENDFUNCTION
线性搜索伪代码:FUNCTION linearSearch(array, target) FOR i ← 0 TO LEN(array)-1 IF array[i] = target THEN RETURN i ENDIF ENDFOR RETURN -1 ENDFUNCTION
Binary search works on sorted arrays only. It repeatedly divides the search interval in half. The algorithm compares the target with the middle element. If they match, the index is returned. If the target is smaller, search the left half; otherwise, search the right half. Its time complexity is O(log n), which is much faster for large datasets.
二分搜索仅适用于已排序的数组。它反复将搜索区间减半。算法将目标与中间元素比较。如果匹配,则返回索引。如果目标更小,搜索左半部分;否则搜索右半部分。它的时间复杂度为 O(log n),对于大型数据集要快得多。
In exams, you may be asked to trace binary search step by step on a given sorted list, showing the low, mid, and high indices at each pass. Be confident in writing both iterative and recursive versions, although iterative is more commonly tested at IGCSE.
在考试中,你可能会被要求对给定的排序列表逐步追踪二分搜索,显示每趟的低、中、高索引。要能够熟练编写迭代和递归版本,尽管在 IGCSE 中迭代版本更常考。
11. Standard Sorting Algorithm: Bubble Sort | 标准排序算法:冒泡排序
The only sorting algorithm explicitly required by OCR is bubble sort. Bubble sort works by repeatedly stepping through the list, comparing adjacent elements and swapping them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, meaning the list is sorted.
OCR 唯一明确要求的排序算法是冒泡排序。冒泡排序通过反复遍历列表,比较相邻元素并在顺序错误时交换它们来工作。遍历列表重复进行,直到不需要交换为止,这意味着列表已排序。
Bubble sort has a time complexity of O(n²) in the worst and average cases, but in the best case (already sorted list) it can be O(n) if the algorithm is optimized to stop early when no swap occurs. Understanding its inefficiency compared to other sorts is a key evaluative point.
冒泡排序在最坏和平均情况下的时间复杂度为 O(n²),但在最佳情况(已排序的列表)下,如果算法优化为在未发生交换时提前停止,可以达到 O(n)。理解它相较于其他排序的低效性是一个关键的评估点。
Pseudocode for an optimized bubble sort:
PROCEDURE bubbleSort(arr)
n ← LEN(arr)
REPEAT
swapped ← FALSE
FOR i ← 0 TO n - 2
IF arr[i] > arr[i+1] THEN
SWAP arr[i], arr[i+1]
swapped ← TRUE
ENDIF
ENDFOR
n ← n - 1
UNTIL swapped = FALSE
ENDPROCEDURE
优化冒泡排序的伪代码:PROCEDURE bubbleSort(arr) n ← LEN(arr) REPEAT swapped ← FALSE FOR i ← 0 TO n-2 IF arr[i] > arr[i+1] THEN SWAP arr[i], arr[i+1]; swapped ← TRUE ENDIF ENDFOR n ← n-1 UNTIL swapped = FALSE ENDPROCEDURE
Practice tracing bubble sort on small arrays. Know how to identify the state of the array after each pass. You might be asked to complete missing parts of pseudocode or to explain why the algorithm's performance degrades with large datasets.
练习在小数组上追踪冒泡排序。知道如何识别每次遍历后数组的状态。你可能会被要求补全伪代码的缺失部分,或解释为什么算法在处理大数据集时性能下降。
12. Testing and Errors | 测试与错误
Testing is essential to ensure an algorithm works correctly. You need to know the difference between syntax errors and logic errors. Syntax errors occur when code violates the grammar rules of the language; logic errors are mistakes in the algorithm's design that produce incorrect results even if the code runs.
测试对于确保算法正确工作至关重要。你需要知道语法错误和逻辑错误的区别。语法错误发生在代码违反语言语法规则时;逻辑错误是算法设计中的错误,即使代码运行也会产生不正确的结果。
Test data is crucial. You should use normal data (typical values that should work), boundary data (values at the edges of acceptable ranges), and erroneous data (invalid inputs). For example, if an algorithm expects a percentage 0–100, test with 0, 100, 50, -1, and 101. Always justify your choice of test data.
测试数据至关重要。你应该使用正常数据(应该有效的典型值)、边界数据(可接受范围边缘的值)和错误数据(无效输入)。例如,如果一个算法期望一个 0–100 的百分比,则用 0、100、50、-1 和 101 进行测试。始终要证明你选择测试数据的理由。
A trace table is a manual technique to follow the execution of an algorithm step by step. Columns represent variables, and rows show the values after each instruction. Trace tables help identify logic errors. Be methodical and always update values after each line of pseudocode.
追踪表是一种手工技术,用于逐步跟踪算法的执行。列代表变量,行显示每条指令后的值。追踪表有助于识别逻辑错误。要有条理,并在伪代码的每一行之后始终更新值。
Finally, understand that thorough testing reduces bugs but cannot prove an algorithm is completely correct in all situations. You should also be able to suggest improvements to an algorithm based on test outcomes.
最后,要理解彻底的测试可以减少错误,但不能证明算法在所有情况下完全正确。你还应该能够根据测试结果提出算法改进建议。
Published by TutorHao | Computer Science Revision Series | aleveler.com
更多咨询请联系16621398022(同微信)

或截图后用「扫一扫 → 相册」选择本图