Computational Thinking and Algorithms计算思维与算法
Computational thinking is the problem-solving mindset that computer scientists bring to every challenge: break a big problem apart (decomposition), spot repeated patterns, strip away irrelevant detail (abstraction), and express the solution as a precise sequence of steps (algorithmic thinking). This guide builds all four pillars from the ground up, then shows you how to write that step sequence as pseudocode and draw it as a flowchart, how to trace through an algorithm by hand so you can verify correctness before writing a single line of code, and how to start reasoning about whether one algorithm is faster than another. Worked examples use real pseudocode and Python throughout.计算思维(computational thinking,计算思维)是计算机科学家解决每一个挑战的思维方式:把大问题拆开(decomposition,分解)、找出重复的模式(pattern recognition,模式识别)、剥去无关细节(abstraction,抽象),再将解决方案表达为一系列精确的步骤(algorithmic thinking,算法思维)。本指南从零开始建立这四个支柱,再展示如何把步骤序列写成伪代码(pseudocode,伪代码)、画成流程图(flowchart,流程图),如何手工追踪一个算法(algorithm,算法)来验证正确性,以及如何初步判断算法效率(efficiency,效率)。全部例题均使用伪代码和 Python 演算。
How to use this guide如何使用本指南
Computational thinking is covered in every curriculum we map to, and all four agree on the core four pillars: decomposition, pattern recognition, abstraction, and algorithmic thinking. Pseudocode and flowcharts are universal. Where they diverge is on algorithm efficiency analysis: Alberta CSE1110 names explicit algorithm testing; Ontario ICS4U strand C adds formal analysis of efficiency (a Grade-12 topic). The tracing and debugging section (§7) maps to Ontario ICS3U strand B, BC Computer Programming 11 "use of test cases," and CSE1110 outcome 1.7. The table tells you which sections are core for you right now; each row cites the curriculum document it was checked against.在我们对照的所有大纲中,计算思维都是核心内容,四套大纲对四个支柱(分解、模式识别、抽象、算法思维)高度一致,伪代码与流程图也是共同要求。它们的分歧在于算法效率分析:阿尔伯塔 CSE1110 明确要求测试算法;安大略 ICS4U C 单元增加了正式的效率分析(属 12 年级内容)。追踪与调试(§7)对应安大略 ICS3U B 单元、BC Computer Programming 11"测试用例的使用"与 CSE1110 结果 1.7。下表告诉你当前哪些节属于你的核心;每行都注明所依据的课纲文件。
| If you are in…如果你在… | Focus on these sections重点学习 | Defer / lighter可推迟 / 减负 | Source依据 |
|---|---|---|---|
| 🇺🇸 US CSTA / AP CSP美国 CSTA / AP CSP | §1 through §7 in full. CSTA 3A-AP-13 (prototypes using algorithms), 3A-AP-17 (decomposition), AP CSP Big Idea 3 topics 3.9 and 3.17 all map to this guide.§1 至 §7 完整学习。CSTA 3A-AP-13(使用算法的原型)、3A-AP-17(分解)、AP CSP 大概念 3 主题 3.9 和 3.17 均对应本指南。 | §5 efficiency analysis is labelled “going deeper” for AP CSP floor students; Big Idea 3 topic 3.17 covers it at the exam level.§5 效率分析对 AP CSP 基础学生标注为"深入";大概念 3 主题 3.17 在考试层面覆盖此内容。 | CSTA K-12 and AP CSP — CSTA 3A-AP-13, 3A-AP-17; AP CSP Big Idea 3 (AAP) topics 3.9, 3.17; Big Idea 1 (CRD) topics 1.3, 1.4— CSTA 3A-AP-13、3A-AP-17;AP CSP 大概念 3(AAP)主题 3.9、3.17;大概念 1(CRD)主题 1.3、1.4 |
| 🇨🇦 ON Grade 11 — ICS3U安大略 11 年级 — ICS3U | §1 through §7 in full. ICS3U Strand B (B1, B3) explicitly names problem-solving strategies including “stepwise refinement, divide and conquer” and algorithm design. §7 tracing maps to B3.3 (algorithm testing) and B4 (software development cycle).§1 至 §7 完整学习。ICS3U B 单元(B1、B3)明确点名"逐步精化、分而治之"等解题策略和算法设计。§7 追踪对应 B3.3(算法测试)和 B4(软件开发周期)。 | Formal efficiency analysis in §5 going-deeper is ICS4U strand C (Grade 12) — skip for ICS3U floor.§5 深入中的正式效率分析属 ICS4U C 单元(12 年级)——ICS3U 基础学生可跳过。 | ON/BC Computer Studies 11-12 — ICS3U Strand B B1, B1.1, B1.3, B3, B3.1, B3.3; ICS4U Strand C C2, C2.1— ICS3U B 单元 B1、B1.1、B1.3、B3、B3.1、B3.3;ICS4U C 单元 C2、C2.1 |
| 🇨🇦 BC — CS10 / CP11BC — CS10 / CP11 | §1 through §7. Computer Studies 10 Content names all four computational-thinking pillars verbatim. Computer Programming 11 Content names “problem decomposition,” “computational thinking processes,” “ways to transform requirements into algorithms.”§1 至 §7。BC Computer Studies 10 内容原文命名全部四个计算思维支柱。Computer Programming 11 内容点名"问题分解"、"计算思维过程"、"将需求转化为算法的方法"。 | BC has no explicit algorithm-efficiency standard at grade 11; §5 going-deeper is enrichment for BC students.BC 11 年级无明确算法效率标准;§5 深入对 BC 学生为拓展内容。 | ON/BC Computer Studies 11-12 — BC CS10 computational-thinking Content bullet; CP11 problem decomposition / computational thinking Content; CP11 Applied Design Curricular Competency (Prototyping)— BC CS10 计算思维内容条目;CP11 问题分解/计算思维内容;CP11 应用设计课程能力(原型制作) |
| 🇨🇦 AB — CSE1010 / CSE1110阿尔伯塔 — CSE1010 / CSE1110 | §1 through §7. CSE1010 introduces the algorithm as a problem-solving tool. CSE1110 outcomes 1.1–1.7 cover algorithm description, analysis, decomposition, pseudocode/structured-chart writing, and algorithm testing — a near-perfect match for this guide.§1 至 §7。CSE1010 将算法作为解决问题的工具引入。CSE1110 结果 1.1–1.7 涵盖算法描述、分析、分解、伪代码/结构图编写和算法测试——与本指南高度契合。 | Formal efficiency comparison (§5 going-deeper) is CSE3110 Advanced — skip for CSE1110 floor students.正式效率比较(§5 深入)属 CSE3110 高级模块——CSE1110 基础学生可跳过。 | Alberta CTS Computing Science — CSE1010 outcomes 1.1.2, 1.2.2; CSE1110 outcomes 1.1–1.7— CSE1010 结果 1.1.2、1.2.2;CSE1110 结果 1.1–1.7 |
| 🇺🇸 AP CSP / AP CSA feeder trackAP CSP / AP CSA 衔接轨道 | All seven sections including going-deeper boxes. AP CSP Big Idea 3 (30–35% of the exam) assumes you can read and write pseudocode fluently. AP CSA (Java OOP) assumes algorithmic thinking and decomposition from day one.全部 7 节,包含深入框。AP CSP 大概念 3(占考试 30–35%)假定你能流畅读写伪代码。AP CSA(Java 面向对象)从第一天起就假定你具备算法思维和分解能力。 | Nothing — this unit is the conceptual bedrock that both AP CS courses build on. See the feeder links in "What This Feeds Into."无 — 本单元是两门 AP CS 课程的概念基石。见"本单元的去向"中的衔接链接。 | CSTA K-12 and AP CSP — AP CSP Big Idea 3 (AAP) exam weighting 30–35%; AP CSP Big Idea 1 (CRD) 10–13%— AP CSP 大概念 3(AAP)考试占比 30–35%;大概念 1(CRD)10–13% |
Once you have located your row, use the two cards below for the speed at which you should work through the recommended sections.找到所在行后,用下面两张卡片决定推进速度。
Memorise four things: the four pillars of computational thinking (decomposition, pattern recognition, abstraction, algorithmic thinking); what an algorithm is and how to write simple pseudocode; how to read a flowchart and follow its logic; and how to trace an algorithm by hand using a variable table. Read every cram-cheat box. Skip the going-deeper efficiency analysis.背熟四件事:计算思维的四个支柱(分解、模式识别、抽象、算法思维);算法是什么以及如何写简单伪代码;如何读懂流程图并遵循其逻辑;如何用变量追踪表手工追踪一个算法。读每个速记框,跳过深入的效率分析。
Always decompose before you code: write the algorithm in plain pseudocode first, trace through it with a worked example to check correctness, then translate to a programming language. AP CSP Big Idea 3 (30–35% of the exam) expects you to read and write the College Board pseudocode fluently. AB CSE1110 and ON ICS3U both require you to test your algorithm with both success and failure cases — §7 shows you how.永远先分解再编码:先用普通伪代码写算法,用例题追踪验证正确性,再翻译为编程语言。AP CSP 大概念 3(占考试 30–35%)要求你能流畅读写 College Board 伪代码。AB CSE1110 和 ON ICS3U 都要求你分别用成功与失败用例测试算法——§7 展示了如何做到这一点。
What is Computational Thinking?什么是计算思维?
- Decomposition分解 — break a complex problem into smaller, manageable sub-problems. BC Computer Studies 10 names this verbatim; AB CSE1110 outcome 1.4 says "decompose the problem into its input, processing and output components."— 将复杂问题拆分为更小、可处理的子问题。BC Computer Studies 10 原文命名此项;AB CSE1110 结果 1.4 要求"将问题分解为输入、处理和输出组件"。
- Pattern recognition模式识别 — spot similarities or repeated structures across problems or within data. Recognising patterns means you can reuse solutions instead of starting from scratch every time.— 发现问题之间或数据内部的相似性或重复结构。识别模式意味着你可以重复使用解决方案,而不必每次从头开始。
- Abstraction抽象 — strip away irrelevant detail and keep only what matters for solving the problem. A map is an abstraction of a city; a function is an abstraction of repeated code.— 去除无关细节,只保留解决问题所需的内容。地图是城市的抽象;函数是重复代码的抽象。
- Algorithmic thinking算法思维 — express the solution as a precise, unambiguous, step-by-step sequence that anyone (or any computer) can follow. This step sequence is called an algorithm.— 将解决方案表达为任何人(或任何计算机)都能遵循的精确、无歧义的逐步序列。这个步骤序列称为算法。
Problem: build an app that helps a student organise their homework across five subjects. Apply all four pillars.问题:创建一个帮助学生在五门学科之间安排作业的应用程序。应用全部四个支柱。
Decomposition.分解。 Break into sub-problems: (1) store the list of subjects and due dates; (2) sort tasks by urgency; (3) display the prioritised list; (4) mark tasks complete. Each is now small enough to code independently.拆分为子问题:(1) 存储学科列表和截止日期;(2) 按紧迫性排序任务;(3) 显示优先列表;(4) 标记任务为已完成。每个子问题现在都足够小,可以独立编码。
Pattern recognition.模式识别。 Sorting tasks by date is the same pattern as sorting any list of items by a key — you can reuse a general sorting approach (covered in the Searching and Sorting guide).按日期排序任务与按键排序任何项目列表的模式相同——你可以重复使用通用排序方法(在搜索与排序指南中介绍)。
Abstraction.抽象。 Represent each task as a record with just three fields: subject, due_date, done. Ignore everything else (teacher name, room number, textbook page) — it is irrelevant to the scheduling problem.将每个任务表示为只有三个字段的记录:subject(学科)、due_date(截止日期)、done(完成状态)。忽略其他一切(教师姓名、教室编号、课本页码)——它们与调度问题无关。
Algorithmic thinking.算法思维。 Write the steps in pseudocode: INPUT tasks with due dates → SORT tasks by due date → FOR EACH task: DISPLAY subject and date → END. Each step is unambiguous and executable.用伪代码写出步骤:输入带截止日期的任务 → 按截止日期排序任务 → 对每个任务:显示学科和日期 → 结束。每个步骤都是无歧义且可执行的。
Going deeper — computational thinking across disciplines深入 — 跨学科的计算思维
CSTA 3A-IC-26 states: "Demonstrate ways a given algorithm applies to problems across disciplines." Computational thinking is not exclusive to programming: a biologist who sequences genomes uses decomposition (break the genome into segments) and pattern recognition (find repeating base sequences). A historian who analyses census data uses abstraction (which fields matter?) and algorithmic thinking (which decades should be compared first?). This cross-disciplinary reach is why every major curriculum includes computational thinking as a core skill, not merely a programming prerequisite.CSTA 3A-IC-26 指出:"展示给定算法在跨学科问题中的应用方式。"计算思维不仅限于编程:对基因组测序的生物学家运用分解(将基因组分成片段)和模式识别(找出重复的碱基序列)。分析人口普查数据的历史学家运用抽象(哪些字段重要?)和算法思维(应该首先比较哪几十年?)。这种跨学科的广泛应用是每个主要课程将计算思维作为核心技能而非仅仅是编程先决条件的原因。
The BC Computer Programming 11 Big Idea "The design cycle includes updating content, tools, and delivery" reinforces this: the four pillars are the design phase of the software development cycle (covered in depth in the Software Development Process guide). Mastering them here pays dividends in every subsequent unit.BC Computer Programming 11 的大概念"设计周期包括更新内容、工具和交付"强化了这一点:四个支柱是软件开发周期的设计阶段(在软件开发过程指南中深入介绍)。在这里掌握它们,在后续每个单元都会带来回报。
Algorithms and Pseudocode算法与伪代码
- Finiteness有限性 — terminates after a finite number of steps (no infinite loops).— 经过有限步骤后终止(无无限循环)。
- Definiteness确定性 — every step is precisely and unambiguously defined; no guessing.— 每个步骤都是精确且无歧义地定义的;不需要猜测。
- Effectiveness有效性 — every step can actually be carried out (no "wave a magic wand").— 每个步骤都可以实际执行(不存在"挥动魔杖")。
INPUT, OUTPUT, IF/ELSE, WHILE, FOR, SET x TO. AB CSE1110 outcome 1.6 says: "write the algorithm in an acceptable format; e.g., pseudocode, structured chart."伪代码是一种人类可读的算法编写方式——结构足够清晰,但不依赖任何特定的编程语言。使用关键字如 INPUT(输入)、OUTPUT(输出)、IF/ELSE(如果/否则)、WHILE(当…时)、FOR(对于)、SET x TO(将 x 设为)。AB CSE1110 结果 1.6 要求:"以可接受的格式编写算法,如伪代码、结构图。"
Problem: given an amount in cents (e.g. 87 cents) and coin denominations [25, 10, 5, 1], write an algorithm that returns the fewest coins needed.问题:给定以分为单位的金额(如 87 分)和硬币面值 [25, 10, 5, 1],编写一个返回所需最少硬币数的算法。
Algorithm in pseudocode.算法(伪代码)。
INPUT amount
SET count TO 0
FOR EACH coin IN [25, 10, 5, 1]:
WHILE amount >= coin:
SET amount TO amount - coin
SET count TO count + 1
OUTPUT count
In the pseudocode above: INPUT reads a value; SET ... TO assigns a value; FOR EACH iterates over each element; WHILE repeats while a condition holds; OUTPUT prints a result.以上伪代码中:INPUT 输入,SET ... TO 赋值,FOR EACH 对每个,WHILE 当…时,OUTPUT 输出。
Python rendering.Python 实现。
def min_coins(amount):
count = 0
for coin in [25, 10, 5, 1]:
while amount >= coin:
amount -= coin
count += 1
return count
print(min_coins(87)) # Output: 6
The pseudocode maps directly to Python: each keyword has a near-identical Python counterpart. Writing pseudocode first means the Python almost writes itself. Output: 87 = 3×25 + 1×10 + 2×1 = 6 coins.伪代码直接映射到 Python:每个关键字都有近乎相同的 Python 对应物。先写伪代码意味着 Python 几乎会自动写出。输出:87 = 3×25 + 1×10 + 2×1 = 6 枚硬币。
SET total TO total + score means what in Python?在伪代码中,SET total TO total + score 对应 Python 中的哪行代码?SET x TO value in pseudocode is an assignment statement. In Python, assignment is written with a single equals sign: total = total + score (or equivalently total += score).伪代码中的 SET x TO value 是赋值语句。在 Python 中,赋值用单等号写:total = total + score(等价地 total += score)。== is a comparison (checks equality); = is assignment (sets a value). Pseudocode SET x TO y always maps to assignment, not comparison.== 是比较(检查是否相等);= 是赋值(设置值)。伪代码 SET x TO y 总是映射到赋值,而不是比较。Going deeper — College Board AP CSP pseudocode vs Python深入 — College Board AP CSP 伪代码与 Python 对比
AP CSP uses its own reference-sheet pseudocode (not Python). Key differences: assignment is written x ← value; lists are 1-indexed (first element is index 1, not 0); Boolean values are TRUE/FALSE; display uses DISPLAY(value); procedures are PROCEDURE name(params). You will see this notation on the AP CSP exam. Learning pseudocode in this guide gives you the underlying concepts; mapping those to the AP CSP notation is a surface-level translation exercise.AP CSP 使用自己的参考手册伪代码(不是 Python)。主要区别:赋值写作 x ← value;列表从 1 开始索引(第一个元素索引为 1,不是 0);布尔值为 TRUE/FALSE;显示使用 DISPLAY(value);过程为 PROCEDURE name(params)。你会在 AP CSP 考试中看到这种表示法。学习本指南中的伪代码让你掌握底层概念;将这些概念映射到 AP CSP 表示法只是一个表层翻译练习。
Flowcharts and Representing Logic流程图与逻辑表示
- Oval / Rounded rectangle椭圆 / 圆角矩形 — Start or End (terminal). Every flowchart must have exactly one Start and at least one End.— 开始或结束(终端)。每个流程图必须恰好有一个开始和至少一个结束。
- Rectangle矩形 — Process / Action (assignment, calculation, I/O operation).— 处理 / 操作(赋值、计算、I/O 操作)。
- Diamond菱形 — Decision (yes/no question). Two arrows leave: one for YES, one for NO.— 判断(是/否问题)。两条箭头离开:一条表示"是",一条表示"否"。
- Parallelogram平行四边形 — Input or Output (read data in, write data out).— 输入或输出(读取数据、写出数据)。
Draw the flowchart for: read a score; if score ≥ 90 output "A"; else if score ≥ 80 output "B"; else output "C or below."画出以下流程图:读入分数;如果分数 ≥ 90 输出"A";否则如果分数 ≥ 80 输出"B";否则输出"C 或以下"。
Pseudocode first (always write this before the flowchart).先写伪代码(总是在画流程图之前写这个)。
START
INPUT score
IF score >= 90 THEN
OUTPUT "A"
ELSE IF score >= 80 THEN
OUTPUT "B"
ELSE
OUTPUT "C or below"
END IF
END
Flowchart structure (described textually).流程图结构(文字描述)。
Oval "START" → Parallelogram "Read score" → Diamond "score ≥ 90?" → YES branch: Parallelogram "Output A" → Oval "END". NO branch → Diamond "score ≥ 80?" → YES branch: Parallelogram "Output B" → Oval "END". NO branch → Parallelogram "Output C or below" → Oval "END". Notice: three possible END ovals (or alternatively one shared END after merging the branches).椭圆"开始"→ 平行四边形"读取分数"→ 菱形"分数 ≥ 90?"→ 是分支:平行四边形"输出 A"→ 椭圆"结束"。否分支 → 菱形"分数 ≥ 80?"→ 是分支:平行四边形"输出 B"→ 椭圆"结束"。否分支 → 平行四边形"输出 C 或以下"→ 椭圆"结束"。注意:三个可能的结束椭圆(或者在合并分支后共享一个结束)。
Sequencing and Step-by-Step Execution顺序结构与逐步执行
- IPO modelIPO 模型 — Input, Process, Output. Every sequential algorithm follows this structure. AB CSE1110 B1.3 and Ontario ICS3U B1.3 both mandate the IPO model as the core problem-solving framework.— 输入(Input)、处理(Process)、输出(Output)。每个顺序算法都遵循这种结构。AB CSE1110 B1.3 和安大略 ICS3U B1.3 都将 IPO 模型规定为核心解题框架。
- Assignment is not comparison赋值不是比较 —
x = 5sets x to 5.x == 5checks if x equals 5. Mixing these up causes logic bugs that are hard to spot.—x = 5将 x 设为 5。x == 5检查 x 是否等于 5。混淆这两者会导致难以发现的逻辑错误。 - Left-to-right / top-to-bottom从左到右 / 从上到下 — the computer executes statements in the order they appear (unless control flow changes this — see §3 and the Control Flow guide).— 计算机按照语句出现的顺序执行(除非控制流改变了这一点——见 §3 和控制流指南)。
Write a sequential algorithm to convert Celsius to Fahrenheit. Formula: F = (C × 9/5) + 32.编写一个顺序算法将摄氏度转换为华氏度。公式:F = (C × 9/5) + 32。
IPO analysis first.首先进行 IPO 分析。
- Input: Celsius temperature (a number).输入:摄氏温度(一个数字)。
- Process: multiply by 9, divide by 5, add 32.处理:乘以 9,除以 5,加 32。
- Output: Fahrenheit temperature.输出:华氏温度。
Pseudocode.伪代码。
INPUT celsius
SET fahrenheit TO (celsius * 9 / 5) + 32
OUTPUT fahrenheit
Python.Python。
celsius = float(input("Enter Celsius: "))
fahrenheit = (celsius * 9 / 5) + 32
print(f"{celsius}°C = {fahrenheit}°F")
Notice: three lines, three phases. The order is critical — you must have the Celsius value before you can compute Fahrenheit; you must have Fahrenheit before you can output it. Swapping lines 1 and 2 causes an error.注意:三行,三个阶段。顺序至关重要——在计算华氏度之前必须有摄氏度值;在输出之前必须有华氏度。交换第 1 行和第 2 行会导致错误。
result after these three lines execute in order?SET x TO 4SET x TO x + 3SET result TO x * 2这三行按顺序执行后,result 的值是多少?SET x TO 4SET x TO x + 3SET result TO x * 2Going deeper — why order of operations in code differs from algebra深入 — 代码中的运算顺序为何与代数不同
In algebra, x = x + 1 is a contradiction (no number equals itself plus one). In programming, it is a valid sequential statement: "take the current value of x, add 1, and store the result back in x." The variable on the left of = names the storage location to write to; the expression on the right is evaluated first using the old value, then the result is stored. This execution model — evaluate right side, assign to left side — is why SET total TO total + score accumulates a running sum correctly. AB CSE1110 outcome 2.4.6 covers "assignment, arithmetical and concatenation and interpolation operators" in this sequential context.在代数中,x = x + 1 是矛盾的(没有数字等于自身加一)。在编程中,这是一个有效的顺序语句:"取 x 的当前值,加 1,将结果存回 x。"= 左边的变量命名要写入的存储位置;右边的表达式首先使用旧值求值,然后将结果存储。这种执行模型——求右边的值,赋值给左边——是 SET total TO total + score 正确累积运行总和的原因。AB CSE1110 结果 2.4.6 在此顺序上下文中涵盖"赋值、算术和拼接与插值运算符"。
Introduction to Algorithm Efficiency算法效率初步
- Linear search线性搜索 — check each item in turn until you find the target. For a list of n items, worst case is n steps.— 依次检查每项,直到找到目标。对于 n 项的列表,最坏情况是 n 步。
- Binary search (sorted list required)二分搜索(需要已排序列表) — check the middle item; eliminate half the list; repeat. For n items, worst case is about log2(n) steps.— 检查中间项;排除一半列表;重复。对于 n 项,最坏情况约为 log2(n) 步。
- Why it matters为什么重要 — for a list of 1,000 items: linear search takes up to 1,000 steps; binary search takes at most 10 steps. For 1,000,000 items: 1,000,000 vs 20 steps.— 对于 1000 项的列表:线性搜索最多需要 1000 步;二分搜索最多需要 10 步。对于 100 万项:1,000,000 步 vs 20 步。
List: [2, 5, 8, 12, 16, 23, 38, 42, 55, 60, 71, 80, 88, 91, 99, 105]. Target: 91. Count steps for each search.列表:[2, 5, 8, 12, 16, 23, 38, 42, 55, 60, 71, 80, 88, 91, 99, 105]。目标:91。计算每种搜索的步骤数。
Linear search (check index 0, 1, 2, … until found).线性搜索(检查索引 0、1、2……直到找到)。
91 at index 13 → checked indices 0-13 → 14 steps
Binary search (list has 16 items; indices 0–15).二分搜索(列表有 16 项;索引 0–15)。
Step 1: mid = 7 → list[7] = 42. 91 > 42 → search upper half [8..15]
Step 2: mid = 11 → list[11] = 80. 91 > 80 → search upper half [12..15]
Step 3: mid = 13 → list[13] = 91. FOUND! → 3 steps
Linear: 14 steps. Binary: 3 steps. For 16 items, log2(16) = 4 (upper bound), and we did it in 3. Binary search is dramatically more efficient on sorted data, but requires the list to be sorted first.线性:14 步。二分:3 步。对于 16 项,log2(16) = 4(上界),我们在 3 步内完成。二分搜索在已排序数据上效率显著更高,但需要先对列表排序。
Going deeper — Big-O notation Honors — ICS4U C2 / CSE3110深入 — 大 O 表示法 荣誉 — ICS4U C2 / CSE3110
Computer scientists use Big-O notation to describe how an algorithm's step count grows as the input size n grows, ignoring constant factors. Common classes: O(1) = constant (same steps regardless of n); O(log n) = logarithmic (binary search); O(n) = linear (linear search); O(n²) = quadratic (simple sorting like bubble sort on an unsorted list). Ontario ICS4U strand C2 ("analyse algorithms for their effectiveness") and Alberta CSE3110 ("compare and contrast the computational efficiencies") are the curricula that assess Big-O reasoning formally. For floor students, "more steps = slower" is sufficient.计算机科学家使用大 O 表示法来描述算法的步骤数随输入大小 n 增长而增长的方式,忽略常数因子。常见类别:O(1) = 常数(无论 n 如何,步骤数相同);O(log n) = 对数(二分搜索);O(n) = 线性(线性搜索);O(n²) = 二次(对未排序列表的简单排序,如冒泡排序)。安大略 ICS4U C 单元第 2 条("分析算法的有效性")和阿尔伯塔 CSE3110("比较计算效率")是正式评估大 O 推理的课程。对于基础学生,"更多步骤 = 更慢"就足够了。
Problem Decomposition: Worked Example问题分解:综合例题
- Level 1第 1 级 — the overall problem statement (one sentence).— 整体问题陈述(一句话)。
- Level 2第 2 级 — the major sub-problems (3–6 items, each a clear responsibility).— 主要子问题(3-6 项,每项职责明确)。
- Level 3第 3 级 — each sub-problem broken into steps small enough to write a pseudocode loop or decision for.— 每个子问题拆分为足够小的步骤,可以为其编写伪代码循环或判断。
Problem: write a program that reads a student's scores on five assignments, computes their average, and prints a letter grade (A ≥ 90, B ≥ 80, C ≥ 70, D ≥ 60, F otherwise). Apply decomposition.问题:编写一个程序,读取学生在五项作业上的成绩,计算平均分,并打印字母等级(A ≥ 90,B ≥ 80,C ≥ 70,D ≥ 60,F 否则)。应用分解。
Level 1 — overall problem.第 1 级——整体问题。 Read 5 scores → compute average → determine letter grade → output.读取 5 个成绩 → 计算平均分 → 确定字母等级 → 输出。
Level 2 — four sub-problems.第 2 级——四个子问题。
- Sub-problem A: Read 5 scores from the user.子问题 A:从用户读取 5 个成绩。
- Sub-problem B: Compute the average of the 5 scores.子问题 B:计算 5 个成绩的平均值。
- Sub-problem C: Map the average to a letter grade.子问题 C:将平均值映射到字母等级。
- Sub-problem D: Output the average and the grade.子问题 D:输出平均分和等级。
Pseudocode (one sub-problem at a time).伪代码(每次一个子问题)。
-- Sub-problem A: Read scores
SET total TO 0
FOR i FROM 1 TO 5:
INPUT score
SET total TO total + score
-- Sub-problem B: Compute average
SET average TO total / 5
-- Sub-problem C: Determine grade
IF average >= 90 THEN SET grade TO "A"
ELSE IF average >= 80 THEN SET grade TO "B"
ELSE IF average >= 70 THEN SET grade TO "C"
ELSE IF average >= 60 THEN SET grade TO "D"
ELSE SET grade TO "F"
-- Sub-problem D: Output
OUTPUT average, grade
Each sub-problem is now a small, independent piece of pseudocode. You can test each part separately before combining them. This is the core benefit of decomposition: independently testable units.每个子问题现在是一小段独立的伪代码。你可以在合并之前分别测试每个部分。这是分解的核心好处:可独立测试的单元。
Tracing and Debugging Algorithms追踪与调试算法
- Variable trace table变量追踪表 — one column per variable, one row per step. Fill in the new value every time a variable changes.— 每个变量一列,每步一行。每次变量更改时填入新值。
- Three types of bugs三种错误类型 — (1) Syntax: code does not parse (typo in a keyword). (2) Logic: code runs but produces the wrong answer. (3) Runtime: code crashes during execution (division by zero, accessing out-of-bounds index).— (1) 语法:代码无法解析(关键字中有错别字)。(2) 逻辑:代码运行但产生错误答案。(3) 运行时:代码在执行过程中崩溃(除以零、访问越界索引)。
- AB CSE1110AB CSE1110 outcome 1.7: "test the algorithm for failure as well as success with appropriate data"; outcome 3.1: "use appropriate test data and debugging techniques to track and correct errors including: run-time errors; logic errors."结果 1.7:"用适当数据测试算法的成功与失败情况";结果 3.1:"使用适当的测试数据和调试技术来跟踪和纠正错误,包括:运行时错误;逻辑错误。"
The following pseudocode is supposed to sum the numbers 1 to 5 and output 15. Trace it and find the bug.以下伪代码本应求 1 到 5 的数字之和并输出 15。追踪它并找出错误。
Buggy pseudocode.有错误的伪代码。
SET total TO 0
SET i TO 1
WHILE i < 5:
SET total TO total + i
SET i TO i + 1
OUTPUT total
Trace table (variable: i, total).追踪表(变量:i,total)。
| Step步骤 | i | total | Condition i < 5?条件 i < 5? |
|---|---|---|---|
| Init初始 | 1 | 0 | — |
| 1 | 1 | 1 | TRUE |
| 2 | 2 | 3 | TRUE |
| 3 | 3 | 6 | TRUE |
| 4 | 4 | 10 | TRUE |
| 5 | 5 | 10 | FALSE → exits |
Bug found.找到错误。 Output is 10, not 15. The condition i < 5 stops the loop before adding 5. Fix: change to i <= 5 (or equivalently i < 6). This is a classic off-by-one logic error.输出是 10,不是 15。条件 i < 5 在添加 5 之前停止了循环。修复:改为 i <= 5(或等价地 i < 6)。这是经典的"差一"逻辑错误。
Going deeper — testing strategies: normal, boundary, and error cases深入 — 测试策略:正常情况、边界情况和错误情况
AB CSE1110 outcome 1.7 says to "test the algorithm for failure as well as success." A thorough test plan covers three categories: (1) Normal cases — typical valid inputs (e.g., score = 75 for a grade calculator). (2) Boundary cases — values at the exact edge of a condition (e.g., score = 90, score = 89, score = 0, score = 100 — each triggers a different branch). (3) Error/exception cases — invalid inputs that the algorithm might encounter (e.g., score = -5, score = 200, score = "hello"). Ontario ICS3U B3.3 says to "design algorithms to detect, intercept, and handle exceptions" — the error cases are what test this. BC Computer Programming 11 says "use of test cases to detect logical or semantic errors." Always test all three categories before calling an algorithm finished.AB CSE1110 结果 1.7 要求"用适当数据测试算法的成功与失败情况"。完整的测试计划涵盖三个类别:(1) 正常情况——典型的有效输入(例如,成绩计算器中的分数 = 75)。(2) 边界情况——恰好在条件边缘的值(例如,分数 = 90、分数 = 89、分数 = 0、分数 = 100——每个触发不同的分支)。(3) 错误/异常情况——算法可能遇到的无效输入(例如,分数 = -5、分数 = 200、分数 = "hello")。安大略 ICS3U B3.3 要求"设计算法以检测、拦截和处理异常"——错误情况正是测试这一点的。BC Computer Programming 11 要求"使用测试用例检测逻辑或语义错误"。在宣布算法完成之前,始终测试所有三个类别。
Exam Strategy and Common Pitfalls考试策略与常见陷阱
- Decompose first, pseudocode second, code third.先分解,再写伪代码,最后写代码。 Never open a code editor before you have a clear pseudocode plan. Students who skip straight to code produce untraceable bugs. The mark-winning habit is: problem → sub-problems → pseudocode → trace → code.在有清晰伪代码计划之前,永远不要打开代码编辑器。跳过直接写代码的学生会产生难以追踪的错误。获得高分的习惯是:问题 → 子问题 → 伪代码 → 追踪 → 代码。
- Label each pillar explicitly.明确标注每个支柱。 On a written exam, when you are asked to apply computational thinking, write the four pillar names (Decomposition, Pattern Recognition, Abstraction, Algorithmic Thinking) and state what each one contributed to your solution. Examiners are checking that you can name and apply all four.在笔试中,当被要求应用计算思维时,写出四个支柱名称(分解、模式识别、抽象、算法思维)并说明每个支柱对你的解决方案的贡献。考官在检查你是否能命名并应用全部四个支柱。
- Trace before you submit.提交前先追踪。 On any pseudocode or algorithm question, always trace through your answer with a concrete example before moving on. A trace table takes 60 seconds and catches most off-by-one and sign errors.在任何伪代码或算法问题上,在继续之前始终用具体示例追踪你的答案。追踪表只需 60 秒,能发现大多数"差一"和符号错误。
- Assignment is not equality.赋值不是等式。
x = 5sets;x == 5tests. In AP CSP pseudocode, assignment uses the arrow (←). Mixing these up is the single most common exam error in this unit.x = 5设置值;x == 5测试相等。在 AP CSP 伪代码中,赋值使用箭头(←)。混淆这两者是本单元最常见的考试错误。 - State the IPO explicitly.明确陈述 IPO。 For any algorithm question: identify the inputs, the processing steps, and the output before writing a single line of pseudocode. Ontario ICS3U B1.3 and AB CSE1110 1.3 both assess this.对于任何算法问题:在写一行伪代码之前,确定输入、处理步骤和输出。安大略 ICS3U B1.3 和 AB CSE1110 1.3 都评估这一点。
- Name the error type.命名错误类型。 When asked about a bug, state whether it is a syntax, logic, or runtime error and justify your answer. Examiners expect the correct term.当被问及错误时,说明它是语法错误、逻辑错误还是运行时错误,并证明你的答案。考官期望正确的术语。
- Binary vs linear search argument.二分搜索与线性搜索的论据。 On efficiency questions: state the pre-condition (list must be sorted for binary), the step count (n vs log n), and a concrete example with numbers. Two sentences scores more than a vague claim.在效率问题上:说明前提条件(二分搜索需要列表已排序)、步骤数(n 对比 log n),以及带数字的具体例子。两句话比模糊的说法得分更高。
- Use pseudocode keywords, not real-language syntax.使用伪代码关键字,而非真实语言语法。 AP CSP uses its own keyword set (DISPLAY, ←, REPEAT, etc.). Write the keywords from the reference sheet, not Python or Java syntax.AP CSP 使用其自己的关键字集(DISPLAY、←、REPEAT 等)。写参考手册中的关键字,而不是 Python 或 Java 语法。
- Show your trace table work.展示你的追踪表工作。 When tracing, draw the table explicitly — column headers are the variable names, rows are the steps. A partial trace that reaches the error is worth more than "I ran it in my head."在追踪时,明确画出表格——列标题是变量名称,行是步骤。到达错误的部分追踪比"我在脑子里运行了它"更有价值。
Flashcards闪卡
Practice Quiz综合测验
SET max TO scores[0] do?伪代码行 SET max TO scores[0] 做什么?SET x TO value is assignment — it stores the value in the variable. This sets max to the first item in the scores list.SET x TO value 是赋值——它将值存储在变量中。这将 max 设置为成绩列表中的第一项。INPUT n | SET result TO 1FOR i FROM 1 TO n: SET result TO result * iOUTPUT result用输入 6 追踪以下伪代码。输出是什么?INPUT n | SET result TO 1FOR i FROM 1 TO n: SET result TO result * iOUTPUT resultReadiness Checklist准备就绪清单
Tick each item when you can do it cold, without notes, on a first attempt.能在无笔记、首次尝试下完成,再勾选每一项。
- Name and define all four pillars of computational thinking (decomposition, pattern recognition, abstraction, algorithmic thinking) and give an example of each applied to a single problem. 🇺🇸 CSTA 3A-AP-17 / 🇨🇦 BC CS10命名并定义计算思维的全部四个支柱(分解、模式识别、抽象、算法思维),并举例说明每个支柱在一个问题中的应用。🇺🇸 CSTA 3A-AP-17 / 🇨🇦 BC CS10
- Write a pseudocode algorithm for a simple problem (e.g., find the maximum of a list) using correct keywords (INPUT, OUTPUT, IF/ELSE, WHILE, FOR, SET...TO). 🇨🇦 AB CSE1110 outcome 1.6为简单问题(如找列表最大值)用正确关键字(INPUT、OUTPUT、IF/ELSE、WHILE、FOR、SET...TO)写出伪代码算法。🇨🇦 AB CSE1110 结果 1.6
- Identify the four flowchart shapes (oval, rectangle, parallelogram, diamond) and state what each represents. Draw a simple flowchart for an if/else decision. 🇨🇦 BC CP11 / ON ICS3U B2.4识别流程图四种形状(椭圆、矩形、平行四边形、菱形)并说明各自代表什么。为一个 if/else 判断画出简单流程图。🇨🇦 BC CP11 / ON ICS3U B2.4
- Explain the IPO model (Input, Process, Output) and apply it to identify the three phases of any simple algorithm before writing code. 🇨🇦 AB CSE1110 outcome 1.3 / ON ICS3U B1.3解释 IPO 模型(输入、处理、输出),并在编写代码之前将其应用于识别任意简单算法的三个阶段。🇨🇦 AB CSE1110 结果 1.3 / ON ICS3U B1.3
- Build a variable trace table for a given pseudocode fragment with at least 5 steps, recording the value of every variable after each step. 🇨🇦 AB CSE1110 outcome 1.7为给定的至少 5 步伪代码片段建立变量追踪表,记录每步后每个变量的值。🇨🇦 AB CSE1110 结果 1.7
- Distinguish between syntax, logic, and runtime errors; identify the type of bug in a given buggy pseudocode fragment and state the fix. 🇨🇦 AB CSE1110 outcome 3.1 / BC CP11区分语法错误、逻辑错误和运行时错误;识别给定有错误的伪代码片段中的错误类型并说明修复方法。🇨🇦 AB CSE1110 结果 3.1 / BC CP11
- Compare linear search and binary search: state the step count (n vs log₂ n), the pre-condition for binary search (sorted list), and give a concrete example with a 16-item list. 🇺🇸 AP CSP 3.17 (AAP-4.A)比较线性搜索和二分搜索:陈述步骤数(n 对比 log₂ n)、二分搜索的前提条件(已排序列表),并用 16 项列表举出具体例子。🇺🇸 AP CSP 3.17 (AAP-4.A)
- Apply the three-pillar testing strategy to a given algorithm: design normal-case, boundary-case, and error-case test inputs and state what the expected output is for each. 🇨🇦 ON ICS3U B3.3 / BC CP11将三支柱测试策略应用于给定算法:设计正常情况、边界情况和错误情况的测试输入,并说明每种情况的预期输出。🇨🇦 ON ICS3U B3.3 / BC CP11
- Decompose a medium-complexity problem (e.g., a grade book, a scheduling app) into at least three sub-problems using top-down design, and write pseudocode for one sub-problem. 🇺🇸 CSTA 3A-AP-17 / 🇨🇦 ON ICS3U B1.1使用自顶向下设计将中等复杂度的问题(如成绩册、调度应用程序)分解为至少三个子问题,并为其中一个子问题编写伪代码。🇺🇸 CSTA 3A-AP-17 / 🇨🇦 ON ICS3U B1.1
- State verbatim (or near-verbatim) the CSTA standards 3A-AP-13 and 3A-AP-17, and explain which pillar of computational thinking each one maps to. 🇺🇸 CSTA Level 3A逐字(或近逐字)陈述 CSTA 标准 3A-AP-13 和 3A-AP-17,并解释每个标准映射到计算思维的哪个支柱。🇺🇸 CSTA Level 3A
- Honors — ICS4U / CSE3110 Explain Big-O notation and classify linear search as O(n) and binary search as O(log n). Argue why O(log n) is dramatically better for large n. 🇨🇦 ON ICS4U C2 / AB CSE3110荣誉 — ICS4U / CSE3110 解释大 O 表示法,将线性搜索归类为 O(n),将二分搜索归类为 O(log n)。论证为何 O(log n) 对于大 n 明显更好。🇨🇦 ON ICS4U C2 / AB CSE3110
What This Feeds Into本单元的去向
Computational thinking is the conceptual bedrock of every topic in this HS Computer Science series. Every subsequent guide (control flow, functions, data structures, OOP) assumes you can decompose a problem before you code, write clear pseudocode, trace an algorithm, and reason informally about efficiency. The two downstream AP courses both assume this unit has already been covered. The links below point at the existing AP CSA guide (which uses Java OOP but depends on exactly the algorithmic thinking taught here) and are provided only for files confirmed to exist in this repo.计算思维是本 HS 计算机科学系列中每个主题的概念基础。后续每本指南(控制流、函数、数据结构、OOP)都假定你能在编码前分解问题、编写清晰的伪代码、追踪算法,并非正式地推理效率。两门下游 AP 课程都假定本单元已经学过。以下链接指向现有 AP CSA 指南(使用 Java OOP,但依赖于这里所教授的算法思维),仅提供已确认存在于本仓库的文件链接。
Within High School Computer Science.在 HS Computer Science 内部。
The next unit (Programming Fundamentals) translates the pseudocode and IPO patterns you learned here into actual variables, data types, and I/O in a real language. Control Flow adds selection and iteration to the sequential step-by-step execution of §4. Functions and Modular Design formalises the decomposition you practiced in §6 into callable sub-programs with parameters. Every later unit reuses the algorithmic thinking and tracing discipline from this guide.下一单元(编程基础)将你在这里学到的伪代码和 IPO 模式转化为真实语言中的实际变量、数据类型和 I/O。控制流在 §4 的顺序逐步执行基础上添加选择和迭代。函数与模块化设计将 §6 中练习的分解正式化为带参数的可调用子程序。每个后续单元都重用本指南中的算法思维和追踪纪律。
AP feeder links (existing in this repo).AP 衔接链接(本仓库中已有)。
The AP CSP feeder (Big Idea 3, Algorithms & Programming, 30–35% of the exam) directly tests pseudocode fluency, decomposition, and algorithm efficiency reasoning covered in this guide. AP CSA (Java-specific, the downstream programming course) assumes you can decompose problems, write clean step-by-step algorithms, and trace code by hand before the first class. Both courses benefit from mastering this unit thoroughly before attempting either AP.AP CSP 衔接(大概念 3,算法与编程,占考试 30–35%)直接测试本指南所涵盖的伪代码流利度、分解和算法效率推理。AP CSA(Java 专项,下游编程课程)假定你在第一堂课之前就能分解问题、编写清晰的逐步算法并手工追踪代码。在尝试任一 AP 课程之前,彻底掌握本单元对两门课程都有益。