High School Computer Science

Searching and Sorting查找与排序

Searching and sorting are the workhorses of computer science: almost every program either looks something up or arranges data in order. This guide builds the two core search algorithms (linear search and binary search), explains why one is dramatically faster on sorted data, introduces algorithm efficiency concepts, then covers the three classic sorts (bubble, selection, insertion) with step-by-step traces and Python code. The final section compares all algorithms and shows how to use Python's built-in sort in practice. Code blocks are in Python/pseudocode throughout; Chinese annotations follow each block.查找(search,查找)与排序(sort,排序)是计算机科学的主力算法:几乎每个程序都需要查找数据或将数据排成顺序。本指南系统讲解两大查找算法(线性查找与二分查找),说明为何二分查找在有序数据上效率显著更高,引入算法效率(algorithm efficiency,算法效率)概念,再逐步演示三种经典排序(冒泡、选择、插入),并附 Python 实现。最后一节比较所有算法并展示如何在实践中使用 Python 内置排序。全部代码使用 Python/伪代码,每段后附中文注解。

7 sections7 节内容 US CSTA · AP CSP · ON · BC · ABUS CSTA · AP CSP · ON · BC · AB Python + pseudocode worked examplesPython + 伪代码例题

How to use this guide如何使用本指南

Searching and sorting are grade-12/Advanced-level topics in all four curricula. Ontario places them exclusively in ICS4U (Grade 12). Alberta names them in CSE3110 (Advanced, 3xxx). CSTA puts efficiency evaluation in Level 3B (the specialist elective stream). BC implies them under Computer Programming 12 "Advanced programming structures." All four use the same core algorithms, so this guide is written for any student who reaches this topic regardless of course code. The Honors flag marks content that maps to formal complexity analysis (ICS4U C2, CSE3110 outcome 1.5.4/1.6.5).查找与排序在四套大纲中均为 12 年级或高级水平内容。安大略将其完全置于 ICS4U(12 年级);阿尔伯塔将其列入 CSE3110(高级模块,3xxx 系列);CSTA 将效率评估置于 3B 级(专业选修流);BC 将其隐含于 Computer Programming 12"高级编程结构"之下。四套大纲的核心算法相同,因此本指南适用于任何到达此主题的学生,无论课程代码如何。荣誉标记(Honors)标注正式复杂度分析内容(对应 ICS4U C2 和 CSE3110 结果 1.5.4/1.6.5)。

If you are in…如果你在… Focus on these sections重点学习 Defer / lighter可推迟 / 减负 Source依据
🇺🇸 US CSTA 3B / AP CSP美国 CSTA 3B / AP CSP §1–§3 core (search + efficiency). AP CSP topic 3.11 tests binary search directly; topic 3.17 tests efficiency. CSTA 3B-AP-10 (classic algorithms) and 3B-AP-11 (evaluate efficiency) are the specialist standards.§1–§3 核心(查找 + 效率)。AP CSP 主题 3.11 直接考查二分查找;主题 3.17 考查效率。CSTA 3B-AP-10(经典算法)和 3B-AP-11(评估效率)是专业标准。 Sort implementation details (§4–§6) are enrichment for AP CSP; AP CSA (Java) covers them formally.排序实现细节(§4–§6)对 AP CSP 为拓展;AP CSA(Java)正式涵盖它们。 CSTA K-12 and AP CSP — CSTA 3B-AP-10, 3B-AP-11; AP CSP 3.11, 3.17; AAP-4.A— CSTA 3B-AP-10、3B-AP-11;AP CSP 3.11、3.17;AAP-4.A
🇨🇦 ON Grade 12 — ICS4U安大略 12 年级 — ICS4U §1 through §7 in full. ICS4U A3.2 (linear + binary search), A3.4 (bubble, insertion, selection sorts), C2.2 (compare search efficiency), C2.3 (compare sort efficiency) all map directly.§1 至 §7 完整学习。ICS4U A3.2(线性+二分查找)、A3.4(冒泡、插入、选择排序)、C2.2(比较查找效率)、C2.3(比较排序效率)均直接对应。 Recursion/merge-sort (A3.6) and formal Big-O derivations (C2.3 full) are ICS4U-only going-deeper material — flagged Honors.递归/归并排序(A3.6)和正式大O推导(C2.3 完整)属 ICS4U-only 深入内容,标注荣誉标记。 ON/BC Computer Studies 11-12 — ICS4U A3.2, A3.4, A3.6, C2.2, C2.3— ICS4U A3.2、A3.4、A3.6、C2.2、C2.3
🇨🇦 BC — CP12BC — CP12 §1 through §7. Computer Programming 12 "Advanced programming structures" and "Management of complexity" are the closest BC anchors; no explicit sorting/searching standard exists.§1 至 §7。BC Computer Programming 12"高级编程结构"和"复杂度管理"是最接近的 BC 依据;没有明确的排序/查找标准。 Formal Big-O analysis is enrichment for BC CP12 students.正式大O分析对 BC CP12 学生为拓展内容。 ON/BC Computer Studies 11-12 — BC CP12 "Advanced programming structures"; "Management of complexity"— BC CP12"高级编程结构";"复杂度管理"
🇨🇦 AB — CSE3110阿尔伯塔 — CSE3110 §1 through §7 in full. CSE3110 outcomes 1.5 (linear + binary search) and 1.6 (bubble, selection, insertion sort) with efficiency comparison (1.5.4, 1.6.5) are a near-perfect match for this guide.§1 至 §7 完整学习。CSE3110 结果 1.5(线性+二分查找)和 1.6(冒泡、选择、插入排序)以及效率比较(1.5.4、1.6.5)与本指南高度契合。 Recursive sorts (quicksort, merge sort) are CSE3310 — outside scope of this guide.递归排序(快速排序、归并排序)属 CSE3310,超出本指南范围。 Alberta CTS Computing Science — CSE3110 outcomes 1.5.1–1.5.4, 1.6.1–1.6.5— CSE3110 结果 1.5.1–1.5.4、1.6.1–1.6.5

Once you have located your row, use the two cards below for the pace at which you should work through the sections.找到所在行后,用下面两张卡片决定推进速度。

!
If you are cramming the night before如果你在临阵磨枪

Memorise five things: linear search scans one-by-one (worst case n steps); binary search halves each time (worst case log₂n steps, sorted list required); bubble sort compares adjacent pairs; selection sort finds the minimum each pass; insertion sort builds a sorted left portion one element at a time. Read every cram-cheat box. Skip the Honors Big-O going-deeper box.背熟五件事:线性查找逐个扫描(最坏 n 步);二分查找每次减半(最坏 log₂n 步,需已排序);冒泡排序比较相邻元素;选择排序每轮找最小值;插入排序逐一将元素插入有序左段。读每个速记框,跳过荣誉大O深入框。

*
If you are going for the top mark如果你目标顶分

Trace every algorithm by hand with a concrete 6–8 element list before moving on. Know the pre-condition, worst-case step count, and best-use scenario for every algorithm. AP CSP Big Idea 3 topic 3.17 (efficiency) and Ontario ICS4U C2.2/C2.3 require you to compare algorithms by step count, not just name them. The Honors going-deeper boxes introduce Big-O notation at the formal level assessed in ICS4U and CSE3110.在继续学习之前,用具体的 6–8 个元素列表手工追踪每个算法。掌握每个算法的前提条件、最坏情况步骤数和最佳使用场景。AP CSP 大概念 3 主题 3.17(效率)和安大略 ICS4U C2.2/C2.3 要求你按步骤数比较算法,而不仅仅是叫出名字。荣誉深入框在 ICS4U 和 CSE3110 正式评估的级别引入大O表示法。

Grade-level note.年级说明。 This is a Grade-12 / Advanced-level topic in all four curricula. If you are in an ICS3U, CSE1110, or AP CSP foundations course, this guide is preview material. The Honors chip marks formal Big-O efficiency analysis (ICS4U C2, CSE3110 outcome 1.5.4/1.6.5). Floor students need only the conceptual step-count comparison in the cram boxes.本单元在四套大纲中均为 12 年级/高级水平内容。如果你在 ICS3U、CSE1110 或 AP CSP 基础课程中,本指南为预习材料。Honors 标记正式大O效率分析(ICS4U C2、CSE3110 结果 1.5.4/1.6.5)。基础学生只需掌握速记框中的直观步骤数比较。

Linear Search线性查找

Linear search — scan every item in order until the target is found.线性查找(线性查找)——按顺序逐个扫描,直到找到目标。
  • Works on适用于 — any list, sorted or unsorted. No pre-condition required.— 任意列表,有序或无序均可,无前提条件。
  • Worst case最坏情况 — n steps (item is at the end or not in the list at all).— n 步(目标在末尾或根本不存在)。
  • Best case最好情况 — 1 step (target is at index 0).— 1 步(目标在索引 0 处)。
  • Return value返回值 — the index where the target was found, or −1 (not found).— 找到目标的索引,或 −1(未找到)。
CSE3110 outcome 1.5.1 names "linear search" explicitly. ICS4U A3.2 says "create linear and binary search algorithms to find data in an array." AP CSP topic 3.11 covers binary search but tests efficiency understanding that requires knowing linear search as the baseline.CSE3110 结果 1.5.1 明确列出"线性查找"。ICS4U A3.2 要求"创建线性和二分查找算法以在数组中查找数据"。AP CSP 主题 3.11 涉及二分查找,但效率理解测试需要以线性查找作为基准。
LS
Worked Example 1 · Linear search on a list of temperatures例题 1 · 在温度列表上线性查找

List: [14, 22, 8, 31, 19, 27, 5]. Search for target = 27. Trace the algorithm step by step.列表:[14, 22, 8, 31, 19, 27, 5]。查找目标 = 27。逐步追踪算法。

Pseudocode.伪代码。

INPUT data, target
FOR i FROM 0 TO length(data) - 1:
    IF data[i] == target THEN
        OUTPUT i
        STOP
OUTPUT -1   # not found

Code keywords above: FOR loop, IF condition, OUTPUT result, early STOP.以上代码关键字:FOR 循环,IF 条件,OUTPUT 输出,提前 STOP 停止。

Python implementation.Python 实现。

def linear_search(data, target):
    for i in range(len(data)):
        if data[i] == target:
            return i
    return -1

temps = [14, 22, 8, 31, 19, 27, 5]
print(linear_search(temps, 27))  # Output: 5

Trace: index 0 (14≠27), index 1 (22≠27), index 2 (8≠27), index 3 (31≠27), index 4 (19≠27), index 5 (27==27) FOUND. Total: 6 comparisons. Worst case for 7 items would be 7.追踪:索引 0(14≠27),索引 1(22≠27),索引 2(8≠27),索引 3(31≠27),索引 4(19≠27),索引 5(27==27)找到。共 6 次比较。7 个元素的最坏情况为 7 次。

A linear search checks a list of 200 items and the target is the very last item. How many comparisons does it make?线性查找检查含 200 个元素的列表,目标恰好是最后一个元素。需要比较多少次?
§1 · Q1
11
100100
200200
88
Linear search checks each item in order. If the target is at the last position (index 199), it must compare all 200 items before finding it. This is the worst case: n comparisons for a list of n items.线性查找按顺序逐个检查。若目标在最后位置(索引 199),则必须比较全部 200 个元素才能找到。这是最坏情况:n 个元素的列表需 n 次比较。
Linear search scans left to right; finding the last item requires checking all n items. Worst case = n comparisons. Binary search would take about log₂(200) ≈ 8 steps (but needs a sorted list).线性查找从左到右扫描;找到最后一项需检查全部 n 个元素。最坏情况 = n 次比较。二分查找约需 log₂(200) ≈ 8 步(但需要已排序列表)。
What does a linear search return when the target is not found in the list?当目标不在列表中时,线性查找返回什么?
§1 · Q2
00
The last index checked最后检查的索引
None / null with no signal无信号地返回 None/null
−1 (a sentinel value meaning “not found”)−1(表示"未找到"的哨兵值)
The conventional return value for "not found" in a search algorithm is −1, because −1 cannot be a valid list index. This is called a sentinel value. Returning 0 would be ambiguous (0 is a valid index). Python's list.index() raises ValueError instead, but the classic algorithm uses −1.查找算法"未找到"的惯用返回值是 −1,因为 −1 不可能是有效列表索引。这称为哨兵值。返回 0 会产生歧义(0 是有效索引)。Python 的 list.index() 改为抛出 ValueError,但经典算法使用 −1。
The standard "not found" sentinel is −1. It cannot be confused with a valid index (all valid indices are ≥ 0).标准"未找到"哨兵是 −1。它不会与有效索引混淆(所有有效索引均 ≥ 0)。
Going deeper — when linear search is the right choice深入 — 何时线性查找是正确选择

Linear search is optimal for unsorted lists because no faster algorithm exists without pre-sorting. It is also correct for small lists (under ~20 items) where the overhead of sorting to enable binary search is not worth it. CSTA 3B-AP-11 says "Evaluate algorithms in terms of their efficiency" — the answer here is: linear search is O(n), simple to implement, and the right tool when you cannot guarantee the list is sorted or when you search only once. If you search the same large sorted list many times, sort once and switch to binary search.对于未排序列表,线性查找是最优选择,因为不预先排序就不存在更快的算法。对于小型列表(约 20 个元素以下),为启用二分查找而排序的开销也不值得。CSTA 3B-AP-11 要求"从效率方面评估算法"——此处的答案是:线性查找是 O(n),实现简单,是列表无法保证有序或只查找一次时的正确工具。若需对同一大型有序列表多次查找,则一次性排序后改用二分查找。


Binary Search二分查找

Binary search — halve the search space on every comparison. Requires a sorted list.二分查找(二分查找)——每次比较将搜索范围减半。必须是已排序列表。
  • Pre-condition前提条件 — list must be sorted in ascending order.— 列表必须按升序排列。
  • Worst case最坏情况 — log₂(n) steps. For 1,024 items: 10 steps. For 1,000,000 items: 20 steps.— log₂(n) 步。1024 个元素:10 步。100 万个元素:20 步。
  • Strategy策略 — compare target to the middle element. If target is smaller, search left half; if larger, search right half; if equal, found.— 将目标与中间元素比较。若目标较小,搜索左半部分;若较大,搜索右半部分;若相等,找到。
CSE3110 outcome 1.5.2 names "binary search." ICS4U A3.2 requires creating it. AP CSP topic 3.11 is "Binary Search" and tests it directly.CSE3110 结果 1.5.2 列出"二分查找"。ICS4U A3.2 要求创建它。AP CSP 主题 3.11 即"二分查找",直接考查。
BS
Worked Example 2 · Binary search on a sorted list例题 2 · 在有序列表上二分查找

Sorted list: [3, 8, 12, 17, 25, 34, 41, 55, 68, 77]. Target = 34. Trace step by step (indices 0–9).有序列表:[3, 8, 12, 17, 25, 34, 41, 55, 68, 77]。目标 = 34。逐步追踪(索引 0–9)。

Pseudocode.伪代码。

INPUT data, target
SET low TO 0
SET high TO length(data) - 1
WHILE low <= high:
    SET mid TO (low + high) // 2
    IF data[mid] == target THEN
        OUTPUT mid
        STOP
    ELSE IF data[mid] < target THEN
        SET low TO mid + 1
    ELSE:
        SET high TO mid - 1
OUTPUT -1   # not found

Code keywords: // is integer division; low/high track the current search window.代码关键字:// 为整除;low/high 跟踪当前搜索窗口。

Step-by-step trace.逐步追踪。

Step步骤lowhighmiddata[mid]data[mid]Action操作
10942525 < 34 → low = 525 < 34 → low = 5
25975555 > 34 → high = 655 > 34 → high = 6
35653434 == 34 → FOUND at index 534 == 34 → 在索引 5 找到

Python implementation.Python 实现。

def binary_search(data, target):
    low, high = 0, len(data) - 1
    while low <= high:
        mid = (low + high) // 2
        if data[mid] == target:
            return mid
        elif data[mid] < target:
            low = mid + 1
        else:
            high = mid - 1
    return -1

nums = [3, 8, 12, 17, 25, 34, 41, 55, 68, 77]
print(binary_search(nums, 34))  # Output: 5

3 comparisons to find 34 in a 10-element list. Linear search would need 6. The advantage grows dramatically for larger lists: a million items takes at most 20 binary-search comparisons vs up to 1,000,000 linear comparisons.在 10 个元素的列表中找到 34 只需 3 次比较;线性查找需要 6 次。对于更大的列表,优势急剧增加:100 万个元素最多需要 20 次二分比较,而线性查找最多需要 100 万次。

Which of the following is a requirement for binary search to work correctly?以下哪项是二分查找正确工作的必要条件?
§2 · Q1
The list must contain only integers列表必须只包含整数
The list must have an even number of elements列表必须有偶数个元素
The list must be sorted in ascending order列表必须按升序排列
The target must always be present in the list目标必须一定在列表中
Binary search works by comparing the target to the middle element and eliminating half the list. This only works if the list is sorted — without ordering, "search the left half" has no meaning. CSE3110 outcome 1.5.4 explicitly requires comparing the "data structures required" for linear vs binary search — the sorted precondition is the key difference.二分查找通过将目标与中间元素比较并淘汰一半列表来工作。只有在列表已排序时才有效——没有顺序,"搜索左半部分"毫无意义。CSE3110 结果 1.5.4 明确要求比较线性与二分查找所需的"数据结构"——已排序前提条件是关键区别。
Binary search requires a sorted list. It works on any comparable type (not just integers), any list length, and correctly returns −1 if the target is absent.二分查找需要已排序列表。它适用于任何可比较类型(不仅仅是整数),任何列表长度,目标不存在时正确返回 −1。
A sorted list has 1,024 items. How many comparisons does binary search need in the worst case?一个已排序列表有 1024 个元素。二分查找在最坏情况下需要多少次比较?
§2 · Q2
512
10
1024
100
Binary search halves the search space each step. Starting from 1,024: 1024 → 512 → 256 → 128 → 64 → 32 → 16 → 8 → 4 → 2 → 1. That is 10 halvings. log₂(1024) = 10 because 2¹⁰ = 1024.二分查找每步将搜索空间减半。从 1024 开始:1024 → 512 → 256 → 128 → 64 → 32 → 16 → 8 → 4 → 2 → 1。共 10 次减半。log₂(1024) = 10,因为 2¹⁰ = 1024。
Binary search takes log₂(n) steps in the worst case. log₂(1024) = 10, since 2¹⁰ = 1024.二分查找最坏情况需 log₂(n) 步。log₂(1024) = 10,因为 2¹⁰ = 1024。
Going deeper — AP CSP exam pseudocode for binary search深入 — AP CSP 考试二分查找伪代码

AP CSP topic 3.11 tests binary search using the College Board's own pseudocode notation. Key differences from the pseudocode in this guide: assignment uses (arrow), lists are 1-indexed (first element is index 1, not 0), and display uses DISPLAY(). The logic is identical; only the surface notation changes. Practice translating the algorithm above into College Board pseudocode as an exam prep exercise. ICS4U C2.2 and CSE3110 outcome 1.5.4 go further: both require formal comparison of computational efficiency (step-count analysis), which maps to the Big-O section (§3) of this guide.AP CSP 主题 3.11 使用 College Board 自己的伪代码表示法考查二分查找。与本指南伪代码的主要区别:赋值用 (箭头),列表从 1 开始索引(第一个元素索引为 1,不是 0),显示用 DISPLAY()。逻辑相同,仅表面符号不同。将上述算法翻译为 College Board 伪代码作为考试准备练习。ICS4U C2.2 和 CSE3110 结果 1.5.4 更进一步:两者均要求对计算效率进行正式比较(步骤数分析),对应本指南第 3 节(大O)。


Introduction to Algorithm Efficiency (Big-O Concept)算法效率初步(大O概念)

Count steps to compare algorithms — more steps = slower.计步比较算法——步骤越多越慢。
  • Linear search step count线性查找步骤数 — proportional to n (the list size). Double the list → double the steps. We say it is O(n).— 与 n(列表大小)成正比。列表翻倍 → 步骤翻倍。称为 O(n)
  • Binary search step count二分查找步骤数 — proportional to log₂(n). Double the list → only one extra step. We say it is O(log n).— 与 log₂(n) 成正比。列表翻倍 → 只多一步。称为 O(log n)
  • Concrete comparison具体比较 — for 1,000,000 items: linear search up to 1,000,000 steps; binary search at most 20 steps. O(log n) is dramatically better for large n.— 100 万个元素:线性查找最多 100 万步;二分查找最多 20 步。对于大 n,O(log n) 显著更优。
AP CSP topic 3.17 (AAP-4.A) and ICS4U C2.2/C2.3 require comparing efficiency. CSE3110 outcomes 1.5.4 and 1.6.5 require "compare and contrast the computational efficiencies." CSTA 3B-AP-11 says "Evaluate algorithms in terms of their efficiency." All four curricula test this.AP CSP 主题 3.17(AAP-4.A)和 ICS4U C2.2/C2.3 要求比较效率。CSE3110 结果 1.5.4 和 1.6.5 要求"比较计算效率"。CSTA 3B-AP-11 要求"从效率方面评估算法"。四套大纲均考查此内容。
WE
Worked Example 3 · Comparing search step counts例题 3 · 比较查找步骤数

For list sizes n = 8, 16, 64, 1024, 1,000,000, calculate the worst-case step count for linear search and binary search side by side.对于列表大小 n = 8、16、64、1024、1,000,000,分别计算线性查找和二分查找的最坏情况步骤数。

nLinear (worst case)线性(最坏)Binary (worst case)二分(最坏)Ratio比值
8832.7×
16164
6464610.7×
1,0241,02410102×
1,000,0001,000,0002050,000×

As n grows, the ratio between linear and binary search becomes enormous. This is the power of O(log n) vs O(n). The cost: binary search requires the list to be sorted first. Sorting itself takes time (O(n²) for simple sorts, O(n log n) for advanced sorts — see §7).随着 n 增大,线性与二分查找之间的比值变得极大。这就是 O(log n) 对比 O(n) 的威力。代价是:二分查找要求列表必须先排序。排序本身需要时间(简单排序 O(n²),高级排序 O(n log n)——见 §7)。

An algorithm takes 100 steps for a list of 1,000 items. If the list grows to 2,000 items and the algorithm still takes about 101 steps, what growth pattern does this suggest?一个算法在 1000 个元素的列表上需 100 步。若列表增至 2000 个元素,算法仍约需 101 步,这说明什么增长模式?
§3 · Q1
Linear growth — O(n)线性增长 — O(n)
Logarithmic growth — O(log n)对数增长 — O(log n)
Quadratic growth — O(n²)二次增长 — O(n²)
Constant growth — O(1)常数增长 — O(1)
When doubling the input size adds only one extra step (100 → 101), that matches logarithmic growth: log₂(1000) ≈ 10 → log₂(2000) ≈ 11. Binary search exhibits exactly this pattern — O(log n).当输入大小翻倍只增加一步(100 → 101)时,符合对数增长:log₂(1000) ≈ 10 → log₂(2000) ≈ 11。二分查找正好表现这种模式——O(log n)。
Linear: doubling n doubles the steps. Logarithmic: doubling n adds only one step. Quadratic: doubling n quadruples the steps. One extra step when n doubles = O(log n).线性:n 翻倍则步骤翻倍。对数:n 翻倍只多一步。二次:n 翻倍则步骤变为四倍。n 翻倍只多一步 = O(log n)。
Why can't you use binary search on the unsorted list [7, 2, 9, 1, 5]?为什么不能对未排序列表 [7, 2, 9, 1, 5] 使用二分查找?
§3 · Q2
The mid-element comparison can eliminate the wrong half, giving an incorrect result中间元素比较可能淘汰错误的一半,导致结果错误
Binary search only works on lists with an even number of elements二分查找只适用于元素数量为偶数的列表
Binary search runs out of memory on unsorted data二分查找在未排序数据上会耗尽内存
Binary search can only be implemented in Java, not Python二分查找只能用 Java 实现,不能用 Python
Binary search works by deciding "target must be in the left half" or "target must be in the right half" based on the mid-element. This is only valid if the list is sorted — on [7,2,9,1,5], the mid element is 9, but that does not tell you which half contains 2. You would eliminate the wrong half and miss the answer.二分查找通过判断"目标一定在左半"或"目标一定在右半"来运作,这只在列表已排序时才成立。对于 [7,2,9,1,5],中间元素是 9,但这无法告知哪半包含 2;你会淘汰错误的一半而错过答案。
The sorted-list requirement is the key constraint. Binary search works on any length, any language. The issue is correctness, not memory or language.已排序列表的要求是关键约束。二分查找适用于任意长度、任何语言。问题在于正确性,而非内存或语言。
Going deeper — Big-O notation formal Honors — ICS4U C2 / CSE3110深入 — 大O表示法正式版 荣誉 — ICS4U C2 / CSE3110

Computer scientists use Big-O notation to describe how an algorithm's step count grows with input size n, ignoring constant factors and lower-order terms. Common classes: O(1) constant (same steps regardless of n — e.g., read index 0); O(log n) logarithmic (binary search); O(n) linear (linear search); O(n log n) linearithmic (efficient sorts — merge sort, quicksort); O(n²) quadratic (simple comparison sorts — bubble, selection, insertion). Ontario ICS4U C2.2/C2.3 and Alberta CSE3110 outcomes 1.5.4/1.6.5 require formal comparison of search and sort efficiencies using this framework. For ICS4U/CSE3110 students: classify each algorithm in this guide by its Big-O, then verify the classification with the step-count table above.计算机科学家使用大O表示法描述算法步骤数随输入大小 n 增长的方式,忽略常数因子和低阶项。常见类别:O(1) 常数(步骤数与 n 无关——如读取索引 0);O(log n) 对数(二分查找);O(n) 线性(线性查找);O(n log n) 线性对数(高效排序——归并排序、快速排序);O(n²) 二次(简单比较排序——冒泡、选择、插入)。安大略 ICS4U C2.2/C2.3 和阿尔伯塔 CSE3110 结果 1.5.4/1.6.5 要求使用此框架对查找和排序效率进行正式比较。ICS4U/CSE3110 学生:为本指南中每个算法分类其大O,然后用上表的步骤数验证分类。


Bubble Sort冒泡排序

Bubble sort — compare adjacent pairs; swap if out of order; repeat until no swaps occur.冒泡排序(冒泡排序)——比较相邻元素;如果顺序错误则交换;重复直到无交换。
  • Passes遍数 — after each pass, the largest unsorted element “bubbles up” to its correct position at the right end.— 每遍后,最大的未排序元素"冒泡"到右端的正确位置。
  • Worst case最坏情况 — O(n²) comparisons — for n=10: up to 45 comparisons; for n=100: up to 4,950.— O(n²) 次比较——n=10 时最多 45 次;n=100 时最多 4950 次。
  • Swap in PythonPython 中的交换 a, b = b, a swaps two variables. No temporary variable needed.a, b = b, a 交换两个变量,无需临时变量。
CSE3110 outcome 1.6.1 names "exchange sort; e.g., bubble sort" explicitly. ICS4U A3.4 says "create a sort algorithm (e.g., bubble, insertion, selection)." Bubble sort is the canonical teaching example because its logic is easy to trace by hand.CSE3110 结果 1.6.1 明确列出"交换排序,如冒泡排序"。ICS4U A3.4 要求"创建排序算法(如冒泡、插入、选择排序)"。冒泡排序是典型的教学示例,因为其逻辑易于手工追踪。
WE
Worked Example 4 · Bubble sort on a 5-element list例题 4 · 5 个元素列表的冒泡排序

Sort [5, 3, 8, 1, 4] into ascending order using bubble sort. Trace each pass.用冒泡排序将 [5, 3, 8, 1, 4] 排成升序,追踪每遍。

Pseudocode.伪代码。

FOR pass FROM 0 TO length(data) - 2:
    FOR i FROM 0 TO length(data) - 2 - pass:
        IF data[i] > data[i+1] THEN
            SWAP data[i] AND data[i+1]

Inner loop compares adjacent pairs; outer loop repeats for each pass. After each pass, the boundary shrinks by 1 (the rightmost element is now sorted).内层循环比较相邻元素;外层循环对每遍重复。每遍后边界缩小 1(最右侧元素已排好)。

Pass-by-pass trace.逐遍追踪。

PassList after pass该遍后列表Swaps交换次数
Start初始[5, 3, 8, 1, 4]
1[3, 5, 1, 4, 8]3
2[3, 1, 4, 5, 8]2
3[1, 3, 4, 5, 8]1
4[1, 3, 4, 5, 8]0 → sorted

Python implementation.Python 实现。

def bubble_sort(data):
    n = len(data)
    for pass_num in range(n - 1):
        for i in range(n - 1 - pass_num):
            if data[i] > data[i + 1]:
                data[i], data[i + 1] = data[i + 1], data[i]  # swap
    return data

print(bubble_sort([5, 3, 8, 1, 4]))  # Output: [1, 3, 4, 5, 8]

The data[i], data[i+1] = data[i+1], data[i] is the Python idiom for swapping two elements.data[i], data[i+1] = data[i+1], data[i] 是 Python 交换两个元素的惯用写法。

After one complete pass of bubble sort on [4, 2, 7, 1, 6], which element is guaranteed to be in its final sorted position?对 [4, 2, 7, 1, 6] 完成一次完整的冒泡排序遍历后,哪个元素一定处于最终排序位置?
§4 · Q1
7 (the largest element, now at the right end)7(最大元素,已移至最右端)
4 (the first element)4(第一个元素)
1 (the smallest element)1(最小元素)
6 (the second largest)6(第二大元素)
After one pass of bubble sort, the largest element has bubbled all the way to the rightmost position and is in its final sorted location. This is why it's called "bubble" sort — the maximum "floats up" to the top (right end) in each pass.一次冒泡排序遍历后,最大元素已完全冒泡到最右端,处于其最终排序位置。这就是"冒泡"排序名称的由来——每遍中最大值"浮"到顶部(右端)。
Bubble sort moves the largest unsorted element to the right end on each pass. After pass 1, the maximum (7) is at index 4 and is done. After pass 2, the second-largest is done. And so on.冒泡排序每遍将最大的未排序元素移至右端。第 1 遍后,最大值(7)在索引 4 处,已完成。第 2 遍后,第二大值完成。以此类推。
Bubble sort has a worst-case of O(n²) comparisons. For n = 10, the maximum number of comparisons is approximately:冒泡排序最坏情况为 O(n²) 次比较。对于 n = 10,最多比较次数约为:
§4 · Q2
10
20
45
100
Bubble sort makes (n-1) + (n-2) + ... + 1 = n(n-1)/2 comparisons in the worst case. For n=10: 10×9/2 = 45. This is O(n²) growth — for n=100 it would be 4,950 comparisons.冒泡排序最坏情况下比较次数为 (n-1) + (n-2) + ... + 1 = n(n-1)/2。n=10 时:10×9/2 = 45。这是 O(n²) 增长——n=100 时将需要 4950 次比较。
Bubble sort worst case = n(n-1)/2 comparisons. For n=10: 9+8+7+6+5+4+3+2+1 = 45. This is O(n²).冒泡排序最坏情况 = n(n-1)/2 次比较。n=10 时:9+8+7+6+5+4+3+2+1 = 45。这是 O(n²)。
Going deeper — optimised bubble sort with early-exit flag深入 — 带提前退出标志的优化冒泡排序

Standard bubble sort always runs all n-1 passes even if the list becomes sorted early. An optimised version adds a swapped flag: if a whole pass makes zero swaps, the list is sorted and the outer loop can exit early. Best case for optimised bubble sort is O(n) — one pass on an already-sorted list. This is an example of the CSTA 3B-AP-11 principle: "Evaluate algorithms in terms of their efficiency" — and then improve them. CSE3110 outcome 1.6.5 requires comparing sorts by computational efficiency, which includes this kind of optimisation analysis.标准冒泡排序总是运行全部 n-1 遍,即使列表提前已排好。优化版添加 swapped(已交换)标志:若某遍未发生任何交换,列表已排好,外层循环可提前退出。优化冒泡排序最好情况为 O(n)——对已排序列表只需一遍。这是 CSTA 3B-AP-11 原则"从效率方面评估算法"的体现,然后加以改进。CSE3110 结果 1.6.5 要求按计算效率比较排序,包括此类优化分析。

def bubble_sort_optimised(data):
    n = len(data)
    for pass_num in range(n - 1):
        swapped = False
        for i in range(n - 1 - pass_num):
            if data[i] > data[i + 1]:
                data[i], data[i + 1] = data[i + 1], data[i]
                swapped = True
        if not swapped:
            break  # list is already sorted
    return data

Selection Sort选择排序

Selection sort — on each pass, find the minimum of the unsorted portion and place it at the front.选择排序(选择排序)——每遍找出未排序部分的最小值,放到前面。
  • Swaps交换次数 — exactly n-1 swaps in the worst case (one per pass) — far fewer swaps than bubble sort.— 最坏情况下恰好 n-1 次交换(每遍一次)——远少于冒泡排序。
  • Comparisons比较次数 — still O(n²) comparisons in all cases (always scans the full remaining portion to find the minimum).— 所有情况下仍为 O(n²) 次比较(总是扫描完整的剩余部分以找最小值)。
  • Position定位 — after pass k, the first k elements are in their final sorted positions.— 第 k 遍后,前 k 个元素处于最终排序位置。
CSE3110 outcome 1.6.2 names "selection sort" explicitly. ICS4U A3.4 includes selection sort. Selection sort is less stable than insertion sort but makes fewer writes to memory.CSE3110 结果 1.6.2 明确列出"选择排序"。ICS4U A3.4 包含选择排序。选择排序的稳定性不如插入排序,但对内存的写入次数更少。
WE
Worked Example 5 · Selection sort on a 5-element list例题 5 · 5 个元素列表的选择排序

Sort [5, 3, 8, 1, 4] using selection sort. Trace each pass.用选择排序将 [5, 3, 8, 1, 4] 排序,追踪每遍。

Pseudocode.伪代码。

FOR i FROM 0 TO length(data) - 2:
    SET min_idx TO i
    FOR j FROM i+1 TO length(data) - 1:
        IF data[j] < data[min_idx] THEN
            SET min_idx TO j
    SWAP data[i] AND data[min_idx]

Inner loop finds the minimum in the unsorted portion (indices i to end). Outer loop places it at position i.内层循环在未排序部分(索引 i 到末尾)中找最小值;外层循环将其放到位置 i。

Pass-by-pass trace.逐遍追踪。

PassMin found找到最小值List after pass该遍后列表
Start初始[5, 3, 8, 1, 4]
1 (i=0)1 (idx 3)[1, 3, 8, 5, 4]
2 (i=1)3 (idx 1)[1, 3, 8, 5, 4]
3 (i=2)4 (idx 4)[1, 3, 4, 5, 8]
4 (i=3)5 (idx 3)[1, 3, 4, 5, 8]

Python implementation.Python 实现。

def selection_sort(data):
    n = len(data)
    for i in range(n - 1):
        min_idx = i
        for j in range(i + 1, n):
            if data[j] < data[min_idx]:
                min_idx = j
        data[i], data[min_idx] = data[min_idx], data[i]  # swap
    return data

print(selection_sort([5, 3, 8, 1, 4]))  # Output: [1, 3, 4, 5, 8]

Selection sort made exactly 4 swaps for 5 elements (n-1 = 4). Bubble sort on the same list made 3+2+1+0 = 6 swaps. Fewer swaps is better when writes to memory are expensive.选择排序对 5 个元素恰好进行了 4 次交换(n-1 = 4)。相同列表的冒泡排序进行了 3+2+1+0 = 6 次交换。当内存写入代价高时,更少交换更好。

After 3 passes of selection sort on a list of 8 elements, how many elements are guaranteed to be in their final sorted positions?对 8 个元素的列表进行 3 遍选择排序后,有多少个元素一定处于最终排序位置?
§5 · Q1
1
2
2
3
Selection sort places exactly one element in its final position per pass. After 3 passes, the 3 smallest elements are at indices 0, 1, and 2, and will not move again. In general: after k passes, the first k elements are fully sorted.选择排序每遍将恰好一个元素放到最终位置。3 遍后,3 个最小元素位于索引 0、1、2,不会再移动。一般规律:k 遍后,前 k 个元素完全排好。
Selection sort: one element finalized per pass. After 3 passes = 3 elements in final positions (the 3 smallest).选择排序:每遍最终确定一个元素。3 遍后 = 3 个元素处于最终位置(最小的 3 个)。
Compared to bubble sort, selection sort makes fewer swaps but the same number of comparisons (O(n²) both). What is the practical advantage of fewer swaps?与冒泡排序相比,选择排序的交换次数更少,但比较次数相同(均为 O(n²))。更少交换的实际优势是什么?
§5 · Q2
Fewer writes to memory, which matters when write operations are expensive (e.g., flash storage)减少内存写入次数,当写入操作代价高昂时(如闪存)这很重要
It makes selection sort faster for every type of data使选择排序对所有类型的数据都更快
It reduces the number of comparisons to O(n)将比较次数减少到 O(n)
It makes selection sort stable使选择排序稳定
Fewer swaps means fewer write operations. On hardware where writes are significantly more expensive than reads (e.g., EEPROM, flash storage, or when data records are large), selection sort's n-1 swaps are preferable to bubble sort's many more swaps. Both are O(n²) for comparisons.更少交换意味着更少写入操作。在写入远比读取昂贵的硬件上(如 EEPROM、闪存,或数据记录较大时),选择排序的 n-1 次交换优于冒泡排序更多次的交换。两者比较次数均为 O(n²)。
Selection sort still has O(n²) comparisons — it is not generally faster in terms of comparisons. The advantage is specifically in write operations. It is also not stable by default.选择排序比较次数仍为 O(n²)——在比较方面并不普遍更快。优势具体在于写入操作。默认情况下也不稳定。

Insertion Sort插入排序

Insertion sort — build a sorted left portion one element at a time, inserting each new element in the correct position.插入排序(插入排序)——每次将一个新元素插入正确位置,逐步构建左侧有序部分。
  • Analogy类比 — like sorting a hand of playing cards: pick up one card at a time and slide it into the right position among already-sorted cards.— 如同整理一手扑克牌:一次拿起一张,插入已排好牌的正确位置。
  • Best case最好情况 — O(n) when the list is already sorted (no shifts needed — just one comparison per element).— O(n),当列表已排好时(无需移位——每个元素只需一次比较)。
  • Worst case最坏情况 — O(n²) when the list is in reverse order (every element must shift all the way left).— O(n²),当列表完全逆序时(每个元素都需一路向左移位)。
CSE3110 outcome 1.6.3 names "insertion sort" explicitly. ICS4U A3.4 includes it. Insertion sort is the most efficient of the three simple sorts for nearly-sorted data and is used internally by Python's built-in sort for small sub-arrays.CSE3110 结果 1.6.3 明确列出"插入排序"。ICS4U A3.4 包含它。插入排序对近乎有序数据是三种简单排序中最高效的,Python 内置排序在小子数组上内部使用插入排序。
WE
Worked Example 6 · Insertion sort on a 5-element list例题 6 · 5 个元素列表的插入排序

Sort [5, 3, 8, 1, 4] using insertion sort. Trace each insertion step.用插入排序将 [5, 3, 8, 1, 4] 排序,追踪每次插入。

Pseudocode.伪代码。

FOR i FROM 1 TO length(data) - 1:
    SET key TO data[i]
    SET j TO i - 1
    WHILE j >= 0 AND data[j] > key:
        SET data[j+1] TO data[j]  # shift right
        SET j TO j - 1
    SET data[j+1] TO key          # insert key

key holds the element being inserted; the WHILE loop shifts larger elements one position right to make room.key 保存待插入元素;WHILE 循环将较大元素向右移一位腾出空间。

Step-by-step trace.逐步追踪。

ikeyList after insertion插入后列表
Start初始[5, 3, 8, 1, 4]
13[3, 5, 8, 1, 4]
28[3, 5, 8, 1, 4]
31[1, 3, 5, 8, 4]
44[1, 3, 4, 5, 8]

Python implementation.Python 实现。

def insertion_sort(data):
    for i in range(1, len(data)):
        key = data[i]
        j = i - 1
        while j >= 0 and data[j] > key:
            data[j + 1] = data[j]  # shift element right
            j -= 1
        data[j + 1] = key          # place key in correct position
    return data

print(insertion_sort([5, 3, 8, 1, 4]))  # Output: [1, 3, 4, 5, 8]

When key=8 (i=2), no shifts were needed because 8 > 5. This means nearly-sorted data has very few shifts, giving O(n) best-case performance — a property bubble sort and selection sort do not have.当 key=8(i=2)时,无需移位,因为 8 > 5。这意味着近乎有序数据的移位极少,达到 O(n) 最好情况性能——这是冒泡排序和选择排序所不具备的特性。

In insertion sort, what happens to elements that are larger than the key being inserted?在插入排序中,比待插入的 key 大的元素会发生什么?
§6 · Q1
They are swapped with the key immediately立即与 key 交换
They are shifted one position to the right to make room for the key向右移一位,为 key 腾出空间
They are moved to the end of the list移到列表末尾
They are deleted and re-inserted later被删除,稍后重新插入
Insertion sort shifts elements one position right (not swaps) to create an empty slot for the key. This is more efficient than swapping because a shift is one write operation while a swap requires three. The key is then placed in the vacated slot.插入排序将元素向右移一位(而非交换)以为 key 创建空槽。这比交换更高效,因为移位是一次写入操作,而交换需要三次。然后将 key 放入腾出的槽中。
Insertion sort uses shifts (not swaps). Each element larger than the key moves right by one position; the key then drops into the gap left behind.插入排序使用移位(不是交换)。每个比 key 大的元素向右移一位;key 随后放入留下的间隙。
For which type of input is insertion sort most efficient (approaching O(n))?对于哪种类型的输入,插入排序最高效(趋近于 O(n))?
§6 · Q2
A list in reverse order完全逆序的列表
A randomly shuffled list随机打乱的列表
A list that is already sorted or nearly sorted已排序或近乎有序的列表
A list with all identical elements所有元素相同的列表
When the list is already sorted, each element's key is greater than or equal to all preceding elements, so the WHILE loop terminates immediately for each i — just one comparison per element, giving O(n) total. A nearly-sorted list has very few shifts needed. This O(n) best-case is insertion sort's key advantage over bubble and selection sort.当列表已排好时,每个元素的 key 都大于等于前面所有元素,因此每个 i 的 WHILE 循环立即终止——每个元素只需一次比较,总计 O(n)。近乎有序的列表只需极少移位。这种 O(n) 最好情况是插入排序相对于冒泡排序和选择排序的关键优势。
Reverse order is the worst case for insertion sort (O(n²) shifts). A sorted list is the best case because no element needs to shift left at all.逆序是插入排序的最坏情况(O(n²) 次移位)。已排序列表是最好情况,因为没有元素需要向左移位。

Comparing Sorts and Using Built-in Sort排序比较与内置排序

Algorithm summary — memorise this table.算法摘要——背熟这张表。
Algorithm算法Best最好Worst最坏Pre-condition前提Notes备注
Linear search线性查找O(1)O(n)NoneSimple; works on any list简单;适用任意列表
Binary search二分查找O(1)O(log n)Sorted list已排序Fast on large sorted data大型有序数据很快
Bubble sort冒泡排序O(n)*O(n²)None*with early-exit optimisation*带提前退出优化
Selection sort选择排序O(n²)O(n²)NoneFewest swaps; good for low-write hardware交换最少;适合少写硬件
Insertion sort插入排序O(n)O(n²)NoneBest for nearly-sorted data近乎有序数据最佳
CSE3110 outcome 1.6.5 and ICS4U C2.3 both require "comparing and contrasting computational efficiencies of different classes of sorts." The table above is your answer framework.CSE3110 结果 1.6.5 和 ICS4U C2.3 均要求"比较不同类别排序的计算效率"。上表即你的答题框架。
WE
Worked Example 7 · Using Python's built-in sort例题 7 · 使用 Python 内置排序

In practice, never use bubble/selection/insertion sort on real data — use Python's built-in sorted() or .sort(). They use Timsort (O(n log n) worst case), which is dramatically faster. Know when to use each method.实践中,切勿对真实数据使用冒泡/选择/插入排序——使用 Python 内置的 sorted().sort()。它们使用 Timsort(最坏情况 O(n log n)),速度显著更快。了解何时使用各种方法。

Python built-in sort examples.Python 内置排序示例。

# sorted() returns a NEW sorted list; original unchanged
nums = [5, 3, 8, 1, 4]
sorted_nums = sorted(nums)
print(sorted_nums)   # [1, 3, 4, 5, 8]
print(nums)          # [5, 3, 8, 1, 4]  -- unchanged

# .sort() sorts IN PLACE; modifies the original list
nums.sort()
print(nums)          # [1, 3, 4, 5, 8]

# Sort descending
nums.sort(reverse=True)
print(nums)          # [8, 5, 4, 3, 1]

# Sort strings or tuples by a key
students = [("Ana", 85), ("Bob", 72), ("Cara", 91)]
students.sort(key=lambda s: s[1])  # sort by score
print(students)  # [('Bob', 72), ('Ana', 85), ('Cara', 91)]

The key=lambda s: s[1] tells Python to sort by the second element (the score). Use sorted() when you need to keep the original; use .sort() when you want to modify in place.key=lambda s: s[1] 告知 Python 按第二个元素(分数)排序。需要保留原列表时用 sorted();想原地修改时用 .sort()

Combining sort + binary search in practice.实践中结合排序与二分查找。

import bisect  # Python standard library for binary search on sorted lists

data = [14, 2, 31, 8, 25, 17]
data.sort()                     # sort first: [2, 8, 14, 17, 25, 31]
pos = bisect.bisect_left(data, 17)  # binary search for 17
print(data[pos] == 17)          # True  (found at index 3)

The pattern: sort once with O(n log n), then use binary search O(log n) for every lookup. Far faster than repeated linear searches on large data.模式:用 O(n log n) 排序一次,再用 O(log n) 的二分查找进行每次查找。对大数据远快于重复线性查找。

You have a list of 10,000 student names already sorted alphabetically. You need to search it 500 times. What is the most efficient approach?你有一个按字母顺序排好的 10000 名学生姓名列表,需要查找 500 次。最高效的方法是什么?
§7 · Q1
Sort the list again before each search using bubble sort每次查找前用冒泡排序重新排序
Use linear search each time because the list is small enough每次使用线性查找,因为列表够小
Sort the list with insertion sort then use linear search用插入排序排序,再使用线性查找
Use binary search directly (list is already sorted) for all 500 searches直接使用二分查找(列表已排好)进行全部 500 次查找
The list is already sorted, so binary search applies immediately. Each search takes at most log₂(10,000) ≈ 14 comparisons. Total: 500 × 14 = 7,000 comparisons. Linear search would take 500 × 10,000 = 5,000,000 comparisons in the worst case. Binary search wins by over 700×.列表已排好,二分查找可直接使用。每次查找最多 log₂(10,000) ≈ 14 次比较。总计:500 × 14 = 7,000 次比较。线性查找最坏情况:500 × 10,000 = 500 万次比较。二分查找胜出 700 余倍。
The list is already sorted — no re-sorting needed. Binary search requires O(log n) per lookup, far cheaper than O(n) linear search for 500 queries on 10,000 items.列表已排好——无需重新排序。二分查找每次查找 O(log n),远比 10000 个元素 500 次查询的 O(n) 线性查找便宜。
In Python, what is the difference between sorted(data) and data.sort()?在 Python 中,sorted(data)data.sort() 有什么区别?
§7 · Q2
sorted() is faster; .sort() is slowersorted() 更快;.sort() 更慢
They are identical in all respects在所有方面完全相同
sorted() returns a new list and leaves the original unchanged; .sort() modifies the original list in place and returns Nonesorted() 返回新列表,原列表不变;.sort() 原地修改原列表并返回 None
sorted() only works on strings; .sort() only works on numberssorted() 只适用于字符串;.sort() 只适用于数字
sorted(data) is a built-in function that returns a new sorted list; the original data is unchanged. data.sort() is a list method that sorts the list in place (modifies data) and returns None. Use sorted() when you need both the original and the sorted version; use .sort() when you only need the sorted result.sorted(data) 是返回新排序列表的内置函数;原始 data 不变。data.sort() 是原地排序列表(修改 data)并返回 None 的列表方法。同时需要原列表和排序版本时用 sorted();只需排序结果时用 .sort()
Speed is the same (both use Timsort). The key difference is in-place vs new-list. Both work on any comparable type.速度相同(均使用 Timsort)。关键区别是原地修改还是返回新列表。两者均适用于任何可比较类型。
Going deeper — why Python's sort is O(n log n) not O(n²) Honors深入 — 为何 Python 内置排序是 O(n log n) 而非 O(n²) 荣誉

Python uses Timsort (invented by Tim Peters, 2002), a hybrid of merge sort and insertion sort. Merge sort divides the list in half, recursively sorts each half, then merges them (O(n log n) in all cases). Insertion sort is used on small runs (<64 elements) because its O(n) best case makes it faster than merge sort for nearly-sorted small arrays. The result: Timsort is O(n) on already-sorted data and O(n log n) in the worst case — far better than the O(n²) algorithms in this guide. ICS4U A3.6 mentions merge sort as a recursive algorithm option. CSE3310 (Recursive Algorithms 1) covers quicksort and merge sort at the Advanced level. For exam purposes, knowing that Python's sort is O(n log n) and why it beats bubble/selection/insertion is sufficient at the ICS4U/CSE3110 level.Python 使用 Timsort(Tim Peters,2002 年发明),是归并排序与插入排序的混合体。归并排序将列表分成两半,递归排序各半部分,再合并(所有情况均为 O(n log n))。对小片段(<64 个元素)使用插入排序,因为其 O(n) 最好情况对近乎有序的小数组比归并排序更快。结果:Timsort 对已排序数据为 O(n),最坏情况为 O(n log n)——远优于本指南的 O(n²) 算法。ICS4U A3.6 将归并排序列为递归算法选项。CSE3310(递归算法 1)在高级模块涵盖快速排序和归并排序。为考试目的,知道 Python 内置排序是 O(n log n) 以及为何它胜过冒泡/选择/插入排序,在 ICS4U/CSE3110 级别已足够。


Exam Strategy and Common Pitfalls考试策略与常见陷阱

Before answering any algorithm question回答任何算法问题前
  • State the pre-condition.陈述前提条件。 Binary search requires a sorted list. Forgetting to state this is the most common exam error. CSE3110 outcome 1.5.4 and ICS4U C2.2 both test whether you know the pre-condition.二分查找需要已排序列表。忘记陈述这一点是最常见的考试错误。CSE3110 结果 1.5.4 和 ICS4U C2.2 均考查你是否知道前提条件。
  • Trace the algorithm with a concrete example.用具体例子追踪算法。 Always run through your answer with a 5–8 element list before submitting. A trace table catches most off-by-one boundary errors in sort algorithms.提交前始终用 5–8 个元素的列表演算答案。追踪表能发现排序算法中大多数"差一"边界错误。
  • Use the correct algorithm name in Chinese and English.用中英文写出正确算法名称。 Examiners expect precise terminology: linear search, binary search, bubble sort, selection sort, insertion sort, algorithm efficiency, time complexity.考官期望精确术语:linear search(线性查找),binary search(二分查找),bubble sort(冒泡排序),selection sort(选择排序),insertion sort(插入排序),algorithm efficiency(算法效率),time complexity(时间复杂度)。
Search questions (§1-§2)查找问题(§1-§2)
  • State pre-condition, step count, and a number example.陈述前提条件、步骤数和数字例子。 "Binary search requires a sorted list. For n=1,024 items, binary search needs at most log₂(1,024)=10 comparisons, versus 1,024 for linear search." That is two sentences and it scores full marks on a typical efficiency question."二分查找需要已排序列表。对于 n=1024 个元素,二分查找最多需要 log₂(1024)=10 次比较,而线性查找最多需要 1024 次。"两句话,典型效率题满分。
  • Trace binary search step by step when asked.被要求时逐步追踪二分查找。 Show the low, high, mid values at each step. Never skip steps on an exam trace question.每步展示 low、high、mid 的值。考试追踪题切勿跳步。
Sort questions (§4-§6)排序问题(§4-§6)
  • Know what changes after each pass.知道每遍后什么发生了变化。 Bubble sort: largest unsorted element moves to its final position. Selection sort: one minimum is placed in final position. Insertion sort: one more element joins the sorted left portion.冒泡排序:最大未排序元素移至最终位置。选择排序:一个最小值放到最终位置。插入排序:多一个元素加入有序左段。
  • Compare using Big-O when asked for efficiency.被问效率时用大O比较。 All three simple sorts are O(n²) worst case for comparisons. Insertion sort is O(n) best case. Python's built-in is O(n log n) worst case. ICS4U C2.3 and CSE3110 outcome 1.6.5 test this comparison explicitly.三种简单排序比较次数最坏情况均为 O(n²)。插入排序最好情况为 O(n)。Python 内置排序最坏情况为 O(n log n)。ICS4U C2.3 和 CSE3110 结果 1.6.5 明确考查此比较。
Answer hygiene作答规范
  • Show all intermediate states in a sort trace.排序追踪中展示所有中间状态。 Draw the list after every pass, not just the final result. Marks are typically awarded per pass on sort-trace questions.每遍后画出列表,不只是最终结果。排序追踪题通常按每遍给分。
  • For AP CSP: use College Board pseudocode.AP CSP 使用 College Board 伪代码。 Assignment uses , lists are 1-indexed, use DISPLAY(). Topic 3.11 binary search and 3.17 efficiency use this notation on the exam.赋值用 ,列表从 1 索引,用 DISPLAY()。主题 3.11 二分查找和 3.17 效率在考试中使用此表示法。

Flashcards闪卡

0 / 14 flipped0 / 14 已翻
Linear search线性查找
Scan every item in order. Worst case: n steps. No pre-condition. Returns −1 if not found.按顺序逐个扫描。最坏情况:n 步。无前提条件。未找到返回 −1。
Binary search二分查找
Halve the search space each step. Worst case: log₂(n) steps. Pre-condition: sorted list. 1024 items → 10 steps.每步将搜索空间减半。最坏情况:log₂(n) 步。前提:已排序列表。1024 个元素 → 10 步。
Why does binary search need a sorted list?二分查找为何需要有序列表?
It eliminates half the list based on whether target is less or greater than mid. Without ordering, this elimination is invalid.它根据目标是否小于/大于中间值来淘汰一半列表。无顺序时淘汰无效。
O(n) vs O(log n)O(n) 与 O(log n)
O(n): linear growth — double n, double steps. O(log n): logarithmic — double n, add only 1 step. For 1M items: 1,000,000 vs 20 steps.O(n):线性增长——n 翻倍,步骤翻倍。O(log n):对数——n 翻倍,只多 1 步。100 万元素:100 万步 vs 20 步。
Bubble sort冒泡排序
Compare adjacent pairs; swap if out of order. Each pass bubbles the largest unsorted element to the right. O(n²) worst case.比较相邻元素;顺序错误则交换。每遍将最大未排序元素冒泡到右端。最坏情况 O(n²)。
Selection sort选择排序
Find minimum of unsorted portion; swap it to the front. Exactly n−1 swaps. O(n²) comparisons always (no early exit).找未排序部分最小值;交换到前面。恰好 n−1 次交换。比较始终 O(n²)(无提前退出)。
Insertion sort插入排序
Build sorted left portion one element at a time; shift larger elements right. Best case O(n) on sorted data; worst case O(n²).每次将一个元素插入有序左段,将较大元素右移。已排序数据最好情况 O(n);最坏 O(n²)。
Which sort has the best best-case?哪种排序的最好情况最优?
Insertion sort: O(n) when already sorted (no shifts needed). Selection sort is always O(n²). Bubble sort O(n) only with early-exit optimisation.插入排序:已排序时 O(n)(无需移位)。选择排序始终 O(n²)。冒泡排序仅在带提前退出优化时为 O(n)。
sorted() vs .sort()sorted() 与 .sort()
sorted(data) returns a new sorted list; original unchanged. data.sort() modifies original in place; returns None.sorted(data) 返回新排序列表;原列表不变。data.sort() 原地修改原列表;返回 None。
Time complexity of Python's built-in sort?Python 内置排序的时间复杂度?
O(n log n) worst case (Timsort). Much better than O(n²) for large n. Always prefer it over manual bubble/selection/insertion sort in production.最坏情况 O(n log n)(Timsort)。对大 n 远优于 O(n²)。实际项目中始终优先使用它,而非手动实现的冒泡/选择/插入排序。
Algorithm efficiency算法效率
How an algorithm's resource use (steps / time) grows with input size n. Compare using step counts or Big-O. CSTA 3B-AP-11; AP CSP 3.17; ICS4U C2; CSE3110 1.5.4.算法资源使用(步骤/时间)随输入大小 n 增长的方式。用步骤数或大O比较。CSTA 3B-AP-11;AP CSP 3.17;ICS4U C2;CSE3110 1.5.4。
log₂(1024) = ?log₂(1024) = ?
10. Because 2¹⁰ = 1024. This means binary search on 1024 items takes at most 10 comparisons.10。因为 2¹⁰ = 1024。这意味着对 1024 个元素进行二分查找最多需要 10 次比较。
Bubble sort: after k passes, what is guaranteed?冒泡排序:k 遍后保证什么?
The k largest elements are in their final positions at the right end of the list.最大的 k 个元素处于列表右端的最终位置。
Selection sort: after k passes, what is guaranteed?选择排序:k 遍后保证什么?
The k smallest elements are in their final sorted positions at the left end of the list.最小的 k 个元素处于列表左端的最终排序位置。

Practice Quiz综合测验

Linear search on an unsorted list of 500 items. The target is not present. How many comparisons are made?对含 500 个元素的未排序列表进行线性查找,目标不存在。需要多少次比较?
Q1
1
9
250
500
When the target is not present, linear search must check every single item before concluding it is absent. 500 items = 500 comparisons. This is the definition of O(n) worst case.目标不存在时,线性查找必须检查每一个元素才能确认不存在。500 个元素 = 500 次比较。这就是 O(n) 最坏情况的定义。
Linear search worst case = n comparisons when target is absent. All 500 must be checked.线性查找最坏情况:目标不存在时需 n 次比较,全部 500 个都需检查。
Sorted list: [1, 5, 9, 12, 18, 24, 30]. Binary search for target = 9. What is the first mid element checked?有序列表:[1, 5, 9, 12, 18, 24, 30]。对目标 9 进行二分查找。第一个检查的中间元素是什么?
Q2
1
9
12
18
List has 7 elements (indices 0–6). mid = (0+6)//2 = 3. list[3] = 12. Since 9 < 12, we search the left half [0..2] next. Total: 2 comparisons to find 9.列表有 7 个元素(索引 0–6)。mid = (0+6)//2 = 3。list[3] = 12。因为 9 < 12,下一步搜索左半部分 [0..2]。共 2 次比较找到 9。
Binary search starts at mid = (low+high)//2 = (0+6)//2 = 3. list[3] = 12. Not 9 yet — then go left.二分查找从 mid = (low+high)//2 = (0+6)//2 = 3 开始。list[3] = 12。还不是 9——然后向左搜索。
One complete pass of bubble sort on [3, 1, 4, 1, 5]. What is the list after the pass?对 [3, 1, 4, 1, 5] 进行一次完整冒泡排序遍历后,列表是什么?
Q3
[1, 3, 1, 4, 5]
[1, 1, 3, 4, 5]
[3, 1, 1, 4, 5]
[1, 3, 4, 1, 5]
Pass: compare (3,1)→swap [1,3,4,1,5]; (3,4) no swap; (4,1)→swap [1,3,1,4,5]; (4,5) no swap. Result: [1,3,1,4,5]. The largest element (5) is now at the right end.遍历:比较 (3,1) → 交换 [1,3,4,1,5];(3,4) 不交换;(4,1) → 交换 [1,3,1,4,5];(4,5) 不交换。结果:[1,3,1,4,5]。最大元素(5)现在在右端。
Bubble sort trace: (3,1) swap → [1,3,4,1,5]; (3,4) no swap; (4,1) swap → [1,3,1,4,5]; (4,5) no swap. One pass puts 5 at the end.冒泡排序追踪:(3,1) 交换 → [1,3,4,1,5];(3,4) 不交换;(4,1) 交换 → [1,3,1,4,5];(4,5) 不交换。一遍将 5 移至末尾。
Which sort algorithm has O(n) best case and O(n²) worst case, making it ideal for nearly-sorted data?哪种排序算法最好情况为 O(n)、最坏情况为 O(n²),使其适合近乎有序的数据?
Q4
Selection sort选择排序
Insertion sort插入排序
Bubble sort without early exit无提前退出的冒泡排序
Linear search线性查找
Insertion sort has O(n) best case when the list is already sorted (zero shifts per element) and O(n²) worst case when reversed. This makes it optimal for nearly-sorted data. Selection sort is always O(n²) — it never benefits from existing order.插入排序最好情况为 O(n)(列表已排序时每个元素零移位),最坏情况为 O(n²)(逆序时)。这使其在近乎有序数据上最优。选择排序始终为 O(n²)——从不受益于现有顺序。
Insertion sort: best O(n), worst O(n²). Selection sort: always O(n²). These are core facts for ICS4U C2.3 and CSE3110 outcome 1.6.5.插入排序:最好 O(n),最坏 O(n²)。选择排序:始终 O(n²)。这是 ICS4U C2.3 和 CSE3110 结果 1.6.5 的核心知识点。
Which CSTA standard says "Evaluate algorithms in terms of their efficiency, correctness, and clarity" (with searching and sorting as example applications)? 🇺🇸 CSTA哪个 CSTA 标准说"从效率、正确性和清晰度方面评估算法"(以查找和排序为示例应用)?🇺🇸 CSTA
Q5
3B-AP-11
3A-AP-13
3B-AP-10
3A-AP-17
CSTA 3B-AP-11 (verbatim): "Evaluate algorithms in terms of their efficiency, correctness, and clarity." The Descriptive Statement explicitly names sorting and searching as examples. 3B-AP-10 is about using classic algorithms; 3B-AP-11 is about evaluating their efficiency.CSTA 3B-AP-11(原文):"从效率、正确性和清晰度方面评估算法。"描述陈述明确将排序和查找列为示例。3B-AP-10 关于使用经典算法;3B-AP-11 关于评估其效率。
3B-AP-11 = evaluate efficiency (with sorting/searching examples). 3B-AP-10 = use and adapt classic algorithms. 3A-AP-13 = create prototypes using algorithms. 3A-AP-17 = decompose problems.3B-AP-11 = 评估效率(以排序/查找为示例)。3B-AP-10 = 使用和调整经典算法。3A-AP-13 = 创建使用算法的原型。3A-AP-17 = 分解问题。
In Ontario ICS4U, which expectation requires creating both linear and binary search algorithms? 🇨🇦 ON在安大略 ICS4U 中,哪项期望要求创建线性和二分查找算法?🇨🇦 ON
Q6
ICS3U B3.1
ICS4U C2.3
ICS4U A3.2
ICS4U A3.4
ICS4U A3.2 (verbatim): "create linear and binary search algorithms to find data in an array." A3.4 is for sort algorithms. C2.2 is for comparing search efficiency. B3.1 is ICS3U, not ICS4U.ICS4U A3.2(原文):"创建线性和二分查找算法以在数组中查找数据。"A3.4 用于排序算法。C2.2 用于比较查找效率。B3.1 是 ICS3U,非 ICS4U。
ICS4U A3.2 = create search algorithms. A3.4 = create sort algorithms. C2.2 = compare search efficiency. C2.3 = compare sort efficiency.ICS4U A3.2 = 创建查找算法。A3.4 = 创建排序算法。C2.2 = 比较查找效率。C2.3 = 比较排序效率。
You sort a list of 10,000 items once with Python's built-in sort. You then do 200 binary searches on it. What is the total complexity class for all operations combined?你用 Python 内置排序对 10000 个元素的列表排序一次,然后进行 200 次二分查找。所有操作合并后的总复杂度类别是什么?
Q7
O(n²) because sorting dominatesO(n²),因为排序占主导
O(n log n) for sorting + O(k log n) for k searches — much better than O(kn) for repeated linear search排序 O(n log n) + k 次查找 O(k log n)——远优于重复线性查找的 O(kn)
O(1) because Python is very fastO(1),因为 Python 非常快
O(n² + k) for sort + searches排序加查找 O(n² + k)
Python's built-in sort is O(n log n). Each binary search is O(log n). 200 binary searches = O(200 log n) = O(k log n). Total: O(n log n + k log n). Compare: repeated linear search = O(kn) = 200 × 10,000 = 2,000,000 ops. Sort + binary = roughly 133,000 + 2,800 = 135,800 ops. A 14× speedup.Python 内置排序为 O(n log n)。每次二分查找为 O(log n)。200 次二分查找 = O(200 log n) = O(k log n)。总计:O(n log n + k log n)。对比:重复线性查找 = O(kn) = 200 × 10,000 = 200 万次操作。排序 + 二分 ≈ 13.3 万 + 2800 = 13.58 万次操作。约 14 倍加速。
Python's built-in sort uses Timsort: O(n log n). Then binary search is O(log n) per query. The combination is O(n log n + k log n), far cheaper than k linear searches at O(kn).Python 内置排序使用 Timsort:O(n log n)。然后每次查询的二分查找为 O(log n)。组合为 O(n log n + k log n),远低于 k 次线性查找的 O(kn)。

Readiness Checklist准备就绪清单

Tick each item when you can do it cold, without notes, on a first attempt.能在无笔记、首次尝试下完成,再勾选每一项。

0 / 11 mastered已掌握 0 / 11

What This Feeds Into本单元的去向

Searching and sorting are foundational to more advanced algorithm study. The linear and binary search algorithms you learned here feed directly into AP CSA and ICS4U, where you implement them in Java. The sorting algorithm comparison (Big-O efficiency analysis) you practiced here is exactly the ICS4U C2.2/C2.3 and CSE3110 1.5.4/1.6.5 assessed content. The link below points to an existing AP CSA study guide confirmed to exist in this repository.查找与排序是更高级算法学习的基础。你在这里学到的线性查找和二分查找算法直接输入 AP CSA 和 ICS4U,在那里用 Java 实现。你在这里练习的排序算法比较(大O效率分析)正是 ICS4U C2.2/C2.3 和 CSE3110 1.5.4/1.6.5 的评估内容。以下链接指向已确认存在于本仓库的 AP CSA 学习指南。

AP feeder link (existing in this repo).AP 衔接链接(本仓库中已有)。

AP CSA Unit 4 · Data Collections (Java: section 4.14 Searching Algorithms + section 4.15 Sorting Algorithms implement and extend exactly the algorithms in this guide using Java arrays and ArrayLists)AP CSA Unit 4 · 数据集合(Java:第 4.14 节查找算法 + 第 4.15 节排序算法使用 Java 数组和 ArrayList 实现并扩展本指南中的算法)

AP CSP Big Idea 3 topic 3.11 (Binary Search) and 3.17 (Algorithmic Efficiency) test the same binary search + efficiency concepts from this guide on the AP CSP exam. AP CSA Unit 4 covers searching and sorting formally in Java including recursive merge sort — this HS guide is the conceptual prerequisite. ICS4U and CSE3110 students: the Big-O analysis in the Honors going-deeper boxes is assessed at your level.AP CSP 大概念 3 主题 3.11(二分查找)和 3.17(算法效率)在 AP CSP 考试中考查与本指南相同的二分查找和效率概念。AP CSA Unit 4 在 Java 中正式涵盖查找和排序,包括递归归并排序——本 HS 指南是概念性先修课。ICS4U 和 CSE3110 学生:荣誉深入框中的大O分析在你的水平被评估。