📚 IGCSE Computer Science: Algorithm Design & Problem-Solving Strategies | IGCSE计算机:算法设计与问题求解策略
In IGCSE Computer Science, an algorithm is a finite set of step-by-step instructions designed to solve a specific problem. Understanding how to design algorithms and apply systematic problem-solving strategies is one of the most important skills you will develop, as it underpins every program you will ever write.
在IGCSE计算机科学中,算法是为解决特定问题而设计的一组有限的逐步指令。理解如何设计算法并运用系统化的问题求解策略,是你将要培养的最重要技能之一,因为它支撑着你未来编写的每一个程序。
1. Understanding Algorithms & the Problem-Solving Cycle | 理解算法与问题求解周期
Before writing any code, a computer scientist must understand the problem deeply and plan a solution carefully. The problem-solving cycle consists of five key stages: analysing the problem, designing an algorithm, writing the program, testing it, and evaluating the solution. Each stage is equally important — skipping straight to coding without a design usually leads to errors and wasted time.
在编写任何代码之前,计算机科学家必须深入理解问题并仔细规划解决方案。问题求解周期包括五个关键阶段:分析问题、设计算法、编写程序、测试程序和评估解决方案。每个阶段同等重要——跳过设计直接编程通常会导致错误和浪费时间。
When analysing a problem, you must identify three things: the input (what data is given), the process (what calculations or operations are required), and the output (what result is expected). For example, a program to calculate the area of a circle has radius as input, the formula πr² as the process, and the area as output.
分析问题时,你必须确定三件事:输入(给出了什么数据)、处理过程(需要什么计算或操作)和输出(期望得到什么结果)。例如,计算圆面积的程序,输入是半径,处理过程是公式πr²,输出是面积。
2. Decomposition | 分解
Decomposition is the process of breaking a large, complex problem into smaller, more manageable sub-problems. Each sub-problem can then be solved independently, and the solutions are combined to solve the original problem. For instance, building a student record system can be decomposed into: entering data, storing data, searching for data, and displaying results.
分解是将一个大型复杂问题拆分成更小、更易于管理的子问题的过程。每个子问题随后可以被独立解决,然后将各解决方案组合起来解决原始问题。例如,构建学生记录系统可以分解为:输入数据、存储数据、搜索数据和显示结果。
Decomposition makes problem-solving easier because small problems are simpler to understand and code. It also makes testing easier — you can test each sub-problem separately. Moreover, if one sub-problem’s solution contains an error, you can isolate and fix it without disturbing the rest of the system.
分解使问题求解更加容易,因为小问题更易于理解和编码。它还使测试更加容易——你可以分别测试每个子问题。此外,如果某个子问题的解决方案包含错误,你可以将其隔离并修复,而不会影响系统的其余部分。
3. Abstraction | 抽象
Abstraction is the process of removing unnecessary details from a problem so that only the essential information remains. In real life, many problems contain irrelevant details that would only confuse a computer program. By abstracting, you focus on what matters and ignore the rest. For example, when writing a program to manage a school library, you might only need each book’s title, author, ISBN, and availability — not its weight, colour, or publisher’s address.
抽象是从问题中移除不必要细节的过程,使只保留关键信息。在现实生活中,许多问题包含只会混淆计算机程序的无关细节。通过抽象,你将注意力集中在重要部分而忽略其余部分。例如,在编写学校图书馆管理程序时,你可能只需要每本书的书名、作者、ISBN号和可借状态——而不需要书的重量、颜色或出版社地址。
Abstraction is what allows humans to manage complex systems. When you draw a flowchart, you abstract away the actual code. When you design a data structure, you abstract away how the data is physically stored. Good abstraction makes programs easier to understand, maintain, and modify.
抽象使人类能够管理复杂系统。当你绘制流程图时,你在抽象掉实际代码。当你设计数据结构时,你在抽象掉数据的物理存储方式。良好的抽象使程序更容易理解、维护和修改。
4. Representing Algorithms: Structure Diagrams | 算法表示:结构图
A structure diagram is a hierarchical visual representation that shows how a complex problem has been decomposed into sub-problems. It resembles a tree, with the main problem at the top and sub-problems branching downward. Structure diagrams are particularly useful during the design stage because they show the overall architecture of a solution at a glance.
结构图是一种分层可视化表示,展示复杂问题如何被分解为子问题。它类似一棵树,主问题位于顶部,子问题向下分支。结构图在设计阶段特别有用,因为它能一目了然地显示解决方案的整体架构。
Consider a program to calculate a student’s final grade. The top-level process might be “Calculate Final Grade.” This decomposes into “obtain coursework mark,” “obtain exam mark,” and “combine marks.” “Obtain exam mark” might further decompose into “read mark from file” and “validate mark.” Each level of decomposition adds more detail until the problem is fully broken down.
考虑一个计算学生期末成绩的程序。顶层过程可能是“计算期末成绩”。它分解为“获取平时成绩”、“获取考试成绩”和“合并成绩”。“获取考试成绩”可能进一步分解为“从文件读取成绩”和“验证成绩”。每一层分解都添加更多细节,直到问题被完全拆解。
5. Representing Algorithms: Flowcharts | 算法表示:流程图
A flowchart is a graphical representation of an algorithm using standard symbols connected by arrows. The key symbols you must know for IGCSE are: the rounded rectangle (terminal/start-stop), the parallelogram (input/output), the rectangle (process/calculation), and the diamond (decision/selection). Each symbol has a strict meaning, and arrows show the flow of control.
流程图是使用标准符号和箭头连接的算法图形表示。IGCSE考试中你必须掌握的关键符号有:圆角矩形(开始/结束)、平行四边形(输入/输出)、矩形(处理/计算)和菱形(判断/选择)。每个符号有严格含义,箭头表示控制流。
例如:为读取一个数字并输出其为正数或负数的程序绘制流程图,包括:开始终端、输入平行四边形、检查数字是否≥0的判断菱形、两条输出路径(“正数”或“负数”)以及结束终端。
Flowcharts are excellent for visual learners and for communicating logic to others. However, for very large algorithms, flowcharts can become sprawling and difficult to follow. In that case, pseudocode is often preferred.
流程图非常适合视觉学习者,也适合向他人传达逻辑。然而,对于非常大的算法,流程图可能变得松散且难以跟踪。在这种情况下,通常更倾向于使用伪代码。
6. Representing Algorithms: Pseudocode | 算法表示:伪代码
Pseudocode is a structured way of describing an algorithm using a mixture of natural language and programming-like constructs. It is not actual code that a computer can run — instead, it is a clear and concise way to express logic that can later be translated into any programming language. CIE provides a specific pseudocode syntax that is used in exam questions.
伪代码是一种结构化描述算法的方式,它混合使用自然语言和类似编程的结构。它不是计算机可以运行的实际代码——而是一种清晰简明地表达逻辑的方式,之后可以被翻译成任何编程语言。CIE考试局提供了在考试题目中使用的特定伪代码语法。
Key pseudocode constructs include: INPUT for reading data, OUTPUT for displaying data, IF…THEN…ELSE…ENDIF for selection, and FOR…NEXT / WHILE…ENDWHILE for iteration. Here is a simple example that reads a number and outputs its double:
关键的伪代码结构包括:INPUT用于读取数据,OUTPUT用于输出数据,IF…THEN…ELSE…ENDIF用于选择,FOR…NEXT / WHILE…ENDWHILE用于循环。下面是一个读取数字并输出其两倍的简单示例:
INPUT Number
Result ← Number × 2
OUTPUT Result
Notice the use of the left-facing arrow ← to represent assignment. Always label variable names clearly and indent nested structures — these habits make your pseudocode readable and earn you marks in examinations.
注意使用左箭头 ← 表示赋值。始终清晰地标出变量名并缩进嵌套结构——这些习惯让伪代码可读性更高,也能在考试中为你赢得分数。
7. Standard Algorithms: Linear Search & Binary Search | 标准算法:线性搜索与二分搜索
The linear search is the simplest search algorithm. It examines each item in a list in order, from the first to the last, until the target is found or the list ends. Its key advantage is that it works on unsorted lists. However, in the worst case, it must examine every single item, making it inefficient for large datasets.
线性搜索是最简单的搜索算法。它按顺序检查列表中的每一项,从第一个到最后一个,直到找到目标或列表结束。其关键优势是它适用于未排序的列表。然而,在最坏情况下,它必须检查每一个项目,这使得它对于大型数据集效率低下。
The binary search is much faster but requires the list to be sorted. It works by repeatedly comparing the target with the middle element. If the target equals the middle, the search is complete. If the target is smaller, the lower half is searched; if larger, the upper half is searched. Each step halves the search space, making binary search extremely efficient — a search among 1,000,000 items takes at most about 20 comparisons.
二分搜索要快得多,但要求列表已排序。它通过反复将目标值与中间元素进行比较来工作。如果目标等于中间元素,搜索完成。如果目标较小,搜索下半部分;如果较大,搜索上半部分。每一步将搜索空间减半,这使得二分搜索极其高效——在1,000,000个项目中搜索最多只需要大约20次比较。
Binary search steps: 1) Find the middle position. 2) Compare target with middle. 3) Discard the half that cannot contain the target. 4) Repeat until found or list empty.
二分搜索步骤:1)找到中间位置。2)将目标与中间值比较。3)丢弃不可能包含目标的一半。4)重复直到找到或列表为空。
8. Standard Algorithms: Bubble Sort & Insertion Sort | 标准算法:冒泡排序与插入排序
The bubble sort works by repeatedly stepping through a list, comparing adjacent pairs, and swapping them if they are in the wrong order. The process repeats until no swaps are needed, meaning the list is sorted. Although simple to understand, bubble sort compares adjacent elements O(n²) times in the worst case, making it slow for large lists. After each pass, the largest unsorted element “bubbles up” to its correct position at the end.
冒泡排序通过反复遍历列表、比较相邻对、如果顺序错误则交换它们来工作。这个过程重复进行,直到不需要任何交换,也就意味着列表已排序。虽然易于理解,但在最坏情况下冒泡排序需要O(n²)次比较相邻元素,对大型列表来说很慢。每轮过后,最大的未排序元素“冒泡”到它末尾的正确位置。
The insertion sort builds a sorted list one item at a time. Starting from the second element, each item is “inserted” into its correct position among the previously sorted elements. This is achieved by shifting larger elements to the right to make space. Insertion sort is more efficient than bubble sort for nearly sorted data and is often compared to how you might sort playing cards in your hand.
插入排序一次构建一个已排序列表。从第二个元素开始,每个项目被“插入”到先前已排序元素中的正确位置。这通过将更大的元素向右移动腾出空间来实现。对于几乎已排序的数据,插入排序比冒泡排序更高效,它常被比作你整理手中扑克牌的方式。
For IGCSE, you must be able to trace both algorithms on a given list, identifying each pass and explaining why the algorithm terminates. You should also know that the number of passes for bubble sort on n items is n − 1 in the worst case.
对于IGCSE,你必须能够在给定列表上跟踪这两种算法,识别每一轮遍历并解释算法为何终止。你还应该知道,在n个项目的列表上,冒泡排序在最坏情况下需要n − 1轮遍历。
9. Algorithm Efficiency | 算法的效率
Efficiency in algorithms refers to how much time and memory a program uses to complete its task. Time efficiency is considered in terms of the number of comparisons or basic operations, while space efficiency refers to the amount of memory required. For IGCSE, you are expected to compare algorithms conceptually rather than use formal Big-O notation, although understanding the idea of growth rates is helpful.
算法中的效率指的是程序完成任务所需的时间和内存。时间效率以比较次数或基本操作数量来衡量,而空间效率指所需的内存量。对于IGCSE,你需要在概念上比较算法,而不是使用正式的Big-O符号,尽管理解增长速率的概念是有帮助的。
Consider the two search algorithms: a linear search on a list of n items might take up to n comparisons in the worst case. A binary search on the same list takes at most log₂n comparisons. For n = 1,000, that is 1,000 comparisons versus only 10. This dramatic difference explains why choosing the right algorithm matters.
考虑两种搜索算法:在n个项目的列表上,线性搜索最坏情况下可能需要n次比较。同一列表上的二分搜索最多需要log₂n次比较。对于n = 1,000,即1,000次比较与仅10次比较的差别。这种巨大差异解释了为什么选择正确的算法很重要。
Deciding which algorithm to use depends on the context. If the data is small or unsorted, a linear search may be perfectly adequate. If the data is large, sorted, and searched frequently, binary search is clearly superior. The cost of sorting a list must also be considered — sorting takes time, so you should not sort data just to perform a single search.
决定使用哪种算法取决于具体情况。如果数据量小或未排序,线性搜索可能完全足够。如果数据量大、已排序且频繁搜索,二分搜索显然更优。排序列表的成本也必须考虑——排序需要时间,所以不应仅仅为了执行一次搜索而排序数据。
10. Boundary Conditions & Testing | 边界条件与测试
Boundary conditions are the extreme values of input that an algorithm may encounter. These include the smallest value, the largest value, and values just inside or just outside the valid range. Testing boundary conditions is crucial because many bugs in computer programs occur exactly when inputs are at the edge of the acceptable range — for example, an array index that is 0 or n−1, or a loop that runs one iteration too many or too few.
边界条件是算法可能遇到的输入极端值。这些包括最小值、最大值,以及恰好位于有效范围之内或之外的值。测试边界条件至关重要,因为计算机程序中的许多错误恰好发生在输入处于可接受范围边缘时——例如,数组索引为0或n−1,或者循环多执行一次或少执行一次迭代。
To test a program thoroughly, you should design test data that covers three categories: normal data (typical values), boundary data (values at the extremes of the valid range), and erroneous data (values outside the valid range). For example, a program that accepts a day of the month (1–31) should be tested with 15, 1, 31, 0, and 32, and you must decide how the program should respond to the erroneous values 0 and 32.
要彻底测试程序,你应设计覆盖三类情况的测试数据:正常数据(典型值)、边界数据(有效范围极端的值)和错误数据(有效范围之外的值)。例如,接受月份中日期(1–31)的程序应使用15、1、31、0和32进行测试,并且你必须决定程序应如何响应错误值0和32。
Validation and verification are two distinct strategies used during testing. Validation checks that input data is reasonable and within acceptable bounds (for example, a negative age is rejected). Verification checks that data is entered correctly — for example, entering a password twice and comparing the two values is a verification technique.
校验和验证是测试中使用的两种不同策略。校验检查输入数据是否合理且在可接受范围内(例如,拒绝负年龄)。验证检查数据是否被正确输入——例如,输入两次密码并比较两个值就是一种验证技术。
11. Trace Tables & Dry Running | 跟踪表与手动模拟
A trace table is a systematic way to record the values of all variables in an algorithm at each step of its execution. By “dry running” an algorithm with a trace table, you can detect logical errors before any code is written. Each row in a trace table corresponds to one line of algorithm execution, showing how every variable changes over time.
跟踪表是一种系统化记录算法执行每一步中所有变量值的方法。通过使用跟踪表“手动模拟”算法,你可以在编写任何代码之前发现逻辑错误。跟踪表中的每一行对应一行算法执行,显示每个变量如何随时间变化。
For example, consider the following pseudocode: total starts at 0, count starts at 1, and a loop adds 3 to total while count advances from 1 to 3. A trace table would have columns titled “count,” “total,” and “output,” and you fill in each row after every loop iteration to verify that the algorithm works correctly. In exams, you will often be given a partial trace table and asked to complete it.
例如,考虑以下伪代码:total从0开始,count从1开始,一个循环在count从1到3的过程中每次向total加3。跟踪表将有列标题“count”、“total”和“output”,你在每次循环迭代后填写每一行,以验证算法是否正常工作。在考试中,你通常会得到部分跟踪表并被要求完成它。
Mastering trace tables will not only earn you marks in the exam but will also make you a significantly better programmer. Tracing through your own code with a table is one of the most reliable debugging techniques that exists.
掌握跟踪表不仅能让你在考试中得分,还能让你成为更好的程序员。用表格跟踪自己的代码是最可靠的调试技术之一。
12. Top-Down Design & Modularity | 自顶向下设计与模块化
Top-down design is a strategy where a problem is first expressed at a high, abstract level and then progressively refined into increasingly detailed sub-problems. This approach is closely linked to decomposition. You start with a broad statement like “produce a payroll report,” and then break it down into “read employee data,” “calculate pay,” “deduct tax,” and “print report.” Each of these is further refined until the design is detailed enough to code directly.
自顶向下设计是一种策略:首先在高抽象层次表达问题,然后逐步细化为越来越详细的子问题。这种方法与分解紧密相连。你从一个宽泛的表述开始,如“生成工资单报告”,然后将其拆分为“读取员工数据”、“计算工资”、“扣除税费”和“打印报告”。其中每一项都进一步细化,直到设计足够详细可以直接编码。
Modularity is the principle that each sub-problem corresponds to a module — a self-contained block of code with a single purpose and a clear interface. Modules can be written and tested independently, which is especially valuable when a team of programmers works on one large project. If a module needs to be changed later, only that module is affected, not the entire system.
模块化是一个原则:每个子问题对应一个模块——一个自包含的代码块,具有单一用途和清晰的接口。模块可以独立编写和测试,这在一个程序员团队协作开发大型项目时尤其有价值。如果后续需要修改一个模块,只有该模块受到影响,而不是整个系统。
Well-designed modules also promote code reuse. A “sort list” module, once written and tested, can be used in many different programs. This saves time, reduces errors, and makes the overall development process more efficient — exactly what the problem-solving cycle is designed to achieve.
设计良好的模块还促进代码复用。一个“排序列表”模块一旦编写和测试完成,可以在许多不同程序中使用。这节省了时间、减少了错误,并使整体开发过程更加高效——这正是问题求解周期设计要达到的目标。
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课程辅导,国外大学本科硕士研究生博士课程论文辅导