Skip to content

Algorithm Design and Problem-solving

A-Level Computer Science · Topic 9

Train
9.1

Computational thinking

Syllabus
Candidates should be able to: Notes and guidance
Show an understanding of abstraction Need for and benefits of using abstraction Describe the purpose of abstraction Produce an abstract model of a system by only including essential details
Describe and use decomposition Break down problems into sub-problems leading to the concept of a program module (procedure / function)

Source: Cambridge International syllabus

Computational thinking 计算思维 is the set of mental tools for analysing a problem and designing a solution a computer can run. Two key ones are abstraction and decomposition.

A part-finished jigsaw puzzle Computational thinking breaks a big problem into smaller, easier parts — like solving a jigsaw

Abstraction

Abstraction 抽象 means keeping the essential features of a problem and ignoring the irrelevant detail, giving a simpler model.

Examples:

  • a train-network map keeps the stations and lines but drops the geography.
  • a class in object-oriented programming keeps only the attributes and methods the system needs.
  • a function hides a piece of work behind a name.

A full model of any real problem would be too big to reason about, so abstraction is essential.

Abstraction turns a cluttered real geography (a wiggly route with scattered buildings) into a clean metro map — evenly spaced station circles on a straight line, keeping the stations and lines and dropping the geography Abstraction keeps the essentials (stations and lines) and drops irrelevant detail (the geography)

Decomposition

Decomposition 分解 means breaking a large problem into smaller sub-problems, each easier to solve and tackled one at a time.

  1. find the main parts of the task.
  2. break each into smaller sub-tasks.
  3. continue until each is small enough to design directly.
  4. solve the small tasks and combine them.

For stock control: "manage stock" → "record sales", "record deliveries", "produce reports" → ("record sales") "look up product", "decrease stock count", "save the transaction". Decomposition makes big problems manageable, lets a team divide the work, and gives modular code — each module becomes a procedure 过程 or function.

A tree with "Manage stock" at the top branching into the modules "Record sales", "Record deliveries" and "Produce reports", and "Record sales" splitting into the sub-tasks "Look up product", "Decrease stock count" and "Save the transaction" Decomposing a program into modules and sub-modules

Explore

Solving a problem the computational way

Step through the four cornerstones in the order you'd use them — break the problem down, spot what repeats, strip it to essentials, then write the steps.

Vocabulary Train
English Chinese Pinyin
computational thinking 计算思维 jì suàn sī wéi
abstraction 抽象 chōu xiàng
decomposition 分解 fēn jiě
procedure 过程 guò chéng
9.2

Algorithms

Syllabus
Candidates should be able to: Notes and guidance
Show understanding that an algorithm is a solution to a problem expressed as a sequence of defined steps
Use suitable identifier names for the representation of data used by a problem and represent these using an identifier table
Write pseudocode that contains input, process and output
Write pseudocode using the three basic constructs of sequence, selection and iteration (repetition)
Document a simple algorithm using a structured English description, a flowchart or pseudocode
Write pseudocode from: • a structured English description • a flowchart
Draw a flowchart from: • a structured English description • pseudocode
Describe and use the process of stepwise refinement to express an algorithm to a level of detail from which the task may be programmed
Use logic statements to define parts of an algorithm solution

Source: Cambridge International syllabus

Bubble sort, pass by pass

An algorithm 算法 is a solution expressed as a sequence of defined steps. Each step is unambiguous 无歧义 (one meaning), deterministic 确定性 (same input → same output), finite (the steps end), and effective (each can be done). An algorithm says what to do, independent of the programming language used to implement it.

Explore

Selection: follow the IF / ELSE branches

Drag the score and watch which branch runs. Selection tests each condition in turn and takes the FIRST one that is true — that is how IF … ELSE IF … ELSE works.

Vocabulary Train
English Chinese Pinyin
algorithm 算法 suàn fǎ
unambiguous 无歧义 wú qí yì
deterministic 确定性 què dìng xìng
9.2

Identifier table

When you start an algorithm, list every piece of data in an identifier table 标识符表 — its variable 变量 name, data type 数据类型, and description:

Variable name Data type Description
Category STRING the product category
SaleDate DATE when the item was sold
ItemCost REAL cost of the item
InStock BOOLEAN TRUE if in stock

Use descriptive names (ItemCost, not x); common types are INTEGER, REAL, STRING, CHAR, BOOLEAN, DATE, plus arrays. The table forces you to name every piece of data before writing code.

An identifier table listing each variable with its name, data type and description, for example ItemCost as a REAL for the cost of the item An identifier table names every piece of data before you write code

Vocabulary Train
English Chinese Pinyin
identifier table 标识符表 biāo shí fú biǎo
variable 变量 biàn liàng
data type 数据类型 shù jù lèi xíng
9.2

Pseudocode — the three basic constructs

Pseudocode 伪代码 is a structured, language-neutral way to describe algorithms.

The three basic constructs as mini-flowcharts: sequence runs step A then B then C; selection tests a condition and does X or Y; iteration repeats a body while a condition holds, looping back The three building blocks of any algorithm: sequence, selection and iteration

1. Sequence

Steps run one after another (sequence 顺序):

INPUT Name
INPUT Age
OUTPUT "Hello", Name

2. Selection

A choice of which steps run, based on a condition (selection 选择):

IF Age >= 18 THEN
    OUTPUT "Adult"
ELSE
    OUTPUT "Minor"
ENDIF

For more options, use CASE OF ... ENDCASE.

3. Iteration

Repeating a block (iteration 迭代, a loop 循环):

FOR i ← 1 TO 10
    OUTPUT i
NEXT i

A WHILE loop tests the condition before each pass (may run zero times); a REPEAT...UNTIL loop tests after each pass (always runs at least once).

Common operations

  • assignment 赋值: x ← 5 (an arrow; = is for comparison).
  • input/output: INPUT variable, OUTPUT expression.
  • comparisons =, <>, <, >, <=, >=; logic AND, OR, NOT.
  • arithmetic + - * /, plus DIV (integer division) and MOD (remainder).
  • strings: LENGTH, LEFT, RIGHT, MID, and & for concatenation 拼接 (joining).

Input → Process → Output

Every program follows this shape:

INPUT Length
INPUT Width
Area ← Length * Width
OUTPUT "Area = ", Area

Listing the inputs and outputs first makes the algorithm cleaner.

Every program follows the shape input, then process, then output, shown with the area example: input the length and width, process by multiplying, output the area Every program follows the Input, Process, Output shape

Explore

IF … ELSE selection

Change the value and watch which branch runs — how a program makes a decision.

Vocabulary Train
English Chinese Pinyin
pseudocode 伪代码 wěi dài mǎ
sequence 顺序 shùn xù
selection 选择 xuǎn zé
iteration 迭代 dié dài
loop 循环 xún huán
assignment 赋值 fù zhí
concatenation 拼接 pīn jiē
9.2

Three notations

The same algorithm can be written three ways.

  • structured English 结构化英语 — natural language with indentation and fixed keywords; good for a high-level description.
  • flowchart 流程图 — a diagram with standard shapes:
Shape Meaning
Rounded rectangle Start / Stop
Parallelogram Input / Output
Rectangle Process
Diamond Decision
Arrow Flow of control
  • pseudocode — the keyword notation above; closest to code.

You should be able to convert between any pair: each IF is a decision diamond, each loop is a back-arrow, and a sequence is stacked rectangles.

A flowchart for averaging numbers: rounded Start and Stop terminators, input/output parallelograms, process rectangles, and a "count < n?" decision diamond whose Yes branch loops back to read the next value A flowchart for averaging a list of numbers, using the standard shapes

Vocabulary Train
English Chinese Pinyin
structured English 结构化英语 jié gòu huà yīng yǔ
flowchart 流程图 liú chéng tú
9.2

Stepwise refinement

Stepwise refinement 逐步求精 starts with a high-level outline and expands each step until it is small enough to code. For an average of $n$ numbers:

Level 1:

Read in the numbers
Compute the average
Output the average

Level 2:

INPUT n
total ← 0
FOR i ← 1 TO n
    INPUT value
    total ← total + value
NEXT i
average ← total / n
OUTPUT average

Each refinement keeps the previous structure and adds detail.

Stepwise refinement: a Level 1 outline (read in the numbers, compute the average, output the average) is expanded into Level 2 detailed pseudocode with the input loop and the division Stepwise refinement: expand each high-level step into detailed pseudocode

Explore

Stepwise refinement: outline to code

Step down the levels. You start with the whole task in one line and keep expanding each step into smaller ones — until every step is simple enough to code directly.

Vocabulary Train
English Chinese Pinyin
stepwise refinement 逐步求精 zhú bù qiú jīng
9.2

Logic statements

A logic statement 逻辑语句 is a Boolean 布尔 condition that controls branching, built from comparisons (x > 10), connectives (AND, OR, NOT) and brackets. Use it as the condition of IF, WHILE or REPEAT...UNTIL:

WHILE attempts < 3 AND NOT loggedIn DO
    INPUT password
    IF password = correctPassword THEN
        loggedIn ← TRUE
    ELSE
        attempts ← attempts + 1
    ENDIF
ENDWHILE

Precedence 优先级 (highest to lowest): NOT, then AND, then OR. Use brackets when unsure. Common mistakes:

  • a = 1 OR 2 is wrong — write a = 1 OR a = 2.
  • NOT a > 5 means NOT (a > 5), i.e. a <= 5.
  • NOT (A AND B) is the same as (NOT A) OR (NOT B) (De Morgan's law 德摩根定律) — handy for simplifying conditions.

A parse tree for "attempts < 3 AND NOT loggedIn": NOT applies to loggedIn first, then AND joins that with attempts < 3 Precedence: NOT binds to loggedIn first, then AND combines the two sides

Worked example. Write an identifier table and pseudocode to read 10 numbers and output the largest. The identifier table names each variable with its data type and purpose: Count : INTEGER (loop counter), Num : REAL (the number just read), Max : REAL (largest so far).

Max ← -999999
FOR Count ← 1 TO 10
    INPUT Num
    IF Num > Max THEN
        Max ← Num
    ENDIF
NEXT Count
OUTPUT Max

The design decision carrying the marks is initialising Max: it must start lower than any possible input - or, safer still, be set to the first number read. Initialise it to 0 and the algorithm wrongly returns 0 for a list of negative numbers, a bug your trace only exposes if the test data include a negative.

Vocabulary Train
English Chinese Pinyin
logic statement 逻辑语句 luó jí yǔ jù
Boolean 布尔 bù ěr
precedence 优先级 yōu xiān jí
De Morgan's law 德摩根定律 dé mó gēn dìng lǜ
9.2

Exam tips

  • Define an algorithm as an unambiguous, finite, deterministic sequence of steps, independent of language.
  • Use the three constructs correctly — sequence, selection, iteration — and keep an identifier table with data types.
  • Break a problem down by decomposition and abstraction, then stepwise refinement.
  • Write pseudocode that would actually run: declare variables and follow the exam's pseudocode style.

Log in or create account

IGCSE & A-Level