← Course Hub← 课程主页 ← All Units← 返回单元列表
H I G H  S C H O O L  C O M P U T E R  S C I E N C E
Practice练习题

Searching and Sorting查找与排序

Practice Questions · AP CSP-Feeder · US / ON / BC / AB Styles练习题集 · AP CSP 衔接 · 美 / 安 / 卑 / 阿省风格

EASY MEDIUM HARD 🇺🇸 US 🇨🇦 ON 🇨🇦 BC 🇨🇦 AB AP CSP-style MCQAP CSP 风格选择题 AP CSP-feeder FRQAP CSP 衔接简答题 ON Provincial-style安大略省考风格 BC Provincial-style卑诗省考风格 AB/Universal Applied阿省/通用应用题 Honors荣誉级


Name:姓名:Date:日期:
PART I  ·  SHORT RESPONSE第一部分  ·  短答题AP CSP-style MCQ + ON/BC/AB short answer · 25 marksAP CSP 风格选择题 + 安/卑/阿省考短答 · 共 25 分

Section A · Short ResponseA 部分 · 短答题

Questions mix multiple-choice and short-answer items. For MCQs, circle the letter and show your reasoning in the work space. For short-answer items, write precise answers. Code is pseudocode or Python; trace each step to verify before answering.本部分包含选择题与短答题。选择题请圈出字母,并在答题空白处写出推理过程。短答题写出精确答案。代码为伪代码或 Python;作答前逐步追踪验证。

Q1 EASY 🇺🇸 US AP CSP-style MCQAP CSP 风格选择题 §1 Linear Search线性查找 · AP CSP 3.11 / CSTA 3A-AP-11 [3 marks][3 分]

A linear search is performed on the list [11, 4, 27, 9, 15, 3, 22] looking for the target value 15. How many comparisons are made before the target is found (including the comparison that finds it)?对列表 [11, 4, 27, 9, 15, 3, 22] 执行线性查找,查找目标值 15。在找到目标之前(含找到时的那次)共进行了多少次比较?

  1. (A) 3
  2. (B) 4
  3. (C) 5
  4. (D) 7
Q2 EASY 🇺🇸 US AP CSP-style MCQAP CSP 风格选择题 §2 Binary Search二分查找 · AP CSP 3.11 / CSE3110 1.5.2 [3 marks][3 分]

Which of the following lists can binary search be applied to correctly without any modification?以下哪个列表可以正确地直接应用二分查找而无需任何修改?

  1. (A) [8, 3, 12, 1, 19]
  2. (B) [19, 12, 8, 3, 1]
  3. (C) [1, 3, 8, 12, 19]
  4. (D) [1, 8, 3, 12, 19]
Q3 MEDIUM 🇨🇦 ON ON Provincial-style安大略省考风格 §1 Linear Search线性查找 · ICS4U A3.2 [5 marks][5 分]

The pseudocode below implements a linear search on the list [6, 14, 2, 31, 8, 20], searching for target 31.以下伪代码在列表 [6, 14, 2, 31, 8, 20] 上执行线性查找,查找目标 31

INPUT data, target
SET found TO False
FOR i FROM 0 TO length(data) - 1:
    IF data[i] == target THEN
        SET found TO True
        OUTPUT i
        STOP
IF found == False THEN
    OUTPUT -1
(a) Complete the trace table below, filling in each row until the loop exits.补全以下追踪表,逐行填写直至循环退出。 [3]
idata[i]data[i] == 31?data[i] == 31?Action操作
(b) State the value output by the program.写出程序输出的值。 [1]
(c) State what the program would output if the target were 99 instead. Explain in one sentence why.若目标改为 99,程序将输出什么?用一句话解释原因。 [1]
Q4 MEDIUM 🇨🇦 AB AB/Universal Applied阿省/通用应用题 §3 Algorithm Efficiency (Big-O)算法效率(大O) · CSE3110 1.5.4 / 1.6.5 [6 marks][6 分]

Consider the four algorithms from this unit and their worst-case step counts for a list of size n.考察本单元四种算法在大小为 n 的列表上的最坏情况步骤数。

(a) Complete the table. Write the Big-O class (e.g., O(n), O(log n), O(n²)) and the worst-case step count for n = 64.补全表格。写出大O类别(如 O(n)、O(log n)、O(n²))以及 n = 64 时的最坏情况步骤数。 [4]
Algorithm算法Big-O (worst)大O(最坏)Steps when n = 64n = 64 时步骤数
Linear search线性查找
Binary search二分查找
Bubble sort冒泡排序
Binary search requires the list to be sorted. State one cost of this requirement in terms of algorithm complexity.二分查找要求列表已排序。从算法复杂度角度说明此要求的一个代价。
(b) A list has 1,048,576 elements (220). How many worst-case comparisons does binary search need? Show your calculation.一个列表有 1,048,576 个元素(220)。二分查找最坏情况需要多少次比较?展示计算过程。 [2]
Q5 MEDIUM 🇨🇦 BC BC Provincial-style卑诗省考风格 §4 Bubble Sort冒泡排序 · CSE3110 1.6.1 [8 marks][8 分]

Bubble sort is applied to the list [7, 2, 9, 4, 6].对列表 [7, 2, 9, 4, 6] 应用冒泡排序。

(a) Complete the trace table showing the state of the list and swap count after each complete pass.补全追踪表,显示每次完整遍历后列表状态和交换次数。 [4]
PassList after pass该遍后列表Swaps this pass本遍交换次数
Start初始[7, 2, 9, 4, 6]--
1
2
3
4
(b) State the total number of swaps made across all passes.写出所有遍次中的交换总次数。 [1]
(c) After pass 1, which element is guaranteed to be in its final sorted position? Explain why.第 1 遍后,哪个元素一定处于最终排序位置?解释原因。 [2]
(d) State the worst-case Big-O complexity of bubble sort.写出冒泡排序最坏情况的大O复杂度。 [1]
PART II  ·  EXTENDED RESPONSE第二部分  ·  简答题AP CSP-feeder FRQ + Honors · 30 marksAP CSP 衔接简答题 + 荣誉级 · 共 30 分

Section B · Extended ResponseB 部分 · 简答题

Show every step of your trace. For trace tables, complete every row. For justification questions, two sentences of clear reasoning earn full marks. Code may be pseudocode or Python.写出每一步追踪过程。追踪表须逐行填写。论证题两句清晰推理即可满分。代码可使用伪代码或 Python。

Q6 EASY 🇺🇸 US AP CSP-feeder FRQAP CSP 衔接简答题 §2 Binary Search二分查找 · AP CSP 3.11 / ICS4U A3.2 [7 marks][7 分]

Binary search is applied to the sorted list [2, 5, 9, 14, 21, 33, 40, 58] (indices 0 to 7) searching for target 21.对有序列表 [2, 5, 9, 14, 21, 33, 40, 58](索引 0 至 7)应用二分查找,查找目标 21

(a) Complete the trace table, filling in low, high, mid, data[mid], and the action taken each step.补全追踪表,填写每步的 lowhighmiddata[mid] 及执行的操作。 [4]
Step步骤lowhighmiddata[mid]Action操作
1
2
3
(b) State how many comparisons were needed to find 21. How many would linear search need in the worst case for the same 8-element list?写出找到 21 所需的比较次数。线性查找在同一 8 元素列表上最坏情况需要多少次? [2]
(c) If the target were 7 (not in the list), state what binary search returns and briefly explain why.若目标为 7(不在列表中),写出二分查找返回什么,并简要解释原因。 [1]
Q7 MEDIUM 🇨🇦 ON ON Provincial-style安大略省考风格 §5 Selection Sort选择排序 · ICS4U A3.4 / CSE3110 1.6.2 [8 marks][8 分]

Selection sort is applied to the list [8, 3, 11, 1, 6].对列表 [8, 3, 11, 1, 6] 应用选择排序。

(a) Complete the trace table for each pass, showing the minimum found and the list state after the swap.补全每遍的追踪表,显示找到的最小值和交换后的列表状态。 [4]
Pass (i)遍(i)Min found (value, idx)找到最小值(值,索引)List after swap交换后列表
Start初始--[8, 3, 11, 1, 6]
0
1
2
3
(b) State the total number of swaps performed by selection sort on this list. Compare this to the maximum number of swaps bubble sort could perform on the same list.写出选择排序在此列表上执行的交换总次数。与冒泡排序在同一列表上可能执行的最大交换次数进行比较。 [2]
(c) After pass i = 2 of selection sort, how many elements are guaranteed to be in their final sorted positions? Identify which elements these are.选择排序第 i = 2 遍后,有多少个元素一定处于最终排序位置?指出是哪些元素。 [2]
Q8 MEDIUM 🇨🇦 BC 🇺🇸 US AP CSP-feeder FRQAP CSP 衔接简答题 §6 Insertion Sort插入排序 · CSE3110 1.6.3 / ICS4U A3.4 [8 marks][8 分]

Insertion sort is applied to the list [6, 2, 10, 4, 8].对列表 [6, 2, 10, 4, 8] 应用插入排序。

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
(a) Complete the trace table for each value of i, showing the key and the list state after the key is inserted.补全每个 i 值对应的追踪表,显示 key 及插入后的列表状态。 [4]
ikeyShifts made移位次数List after insertion插入后列表
Start初始----[6, 2, 10, 4, 8]
1
2
3
4
(b) State the total number of shift operations performed across all iterations.写出所有迭代中执行的移位操作总次数。 [1]
(c) Explain why insertion sort is more efficient than bubble or selection sort when the input is already nearly sorted. Refer to the WHILE loop condition in your answer.解释为什么当输入已近乎有序时,插入排序比冒泡或选择排序更高效。在答案中引用 WHILE 循环条件。 [2]
(d) State the best-case and worst-case Big-O complexity of insertion sort and describe the input that causes each case.写出插入排序最好情况和最坏情况的大O复杂度,并描述导致各情况的输入特征。 [1]
Q9 HARD Honors荣誉级 🇺🇸 US AP CSP-feeder FRQAP CSP 衔接简答题 §3 + §7 Big-O + Algorithm Selection大O + 算法选择 · CSTA 3B-AP-11 / ICS4U C2.2 [7 marks][7 分]

A student must search a dataset of n = 1,000,000 records. They can either (1) use linear search directly, or (2) first sort the list using a simple O(n²) sort, then use binary search. They plan to perform exactly 10 searches total.一名学生需要搜索包含 n = 1,000,000 条记录的数据集。他们可以选择(1)直接使用线性查找,或(2)先用 O(n²) 的简单排序对列表排序,再使用二分查找。他们计划总共执行 10 次搜索。

(a) Calculate the total worst-case number of comparisons for option 1 (all 10 linear searches).计算方案 1(全部 10 次线性查找)的最坏情况总比较次数。 [1]
(b) Calculate the total worst-case number of steps for option 2: the sort plus all 10 binary searches. Use log₂(1,000,000) ≈ 20.计算方案 2 的最坏情况总步骤数:一次排序加上全部 10 次二分查找。使用 log₂(1,000,000) ≈ 20。 [2]
(c) Based on your calculations, which option is better for 10 searches? Which would be better if they planned 1,000 searches instead? Justify both answers.根据计算,对于 10 次搜索哪个方案更好?若计划 1,000 次搜索又如何?为两个答案提供理由。 [2]
(d) In Python, the built-in sorted() uses Timsort (O(n log n) worst case). Recalculate option 2 with Timsort (n log n = 1,000,000 x 20 steps for the sort). Does this change your recommendation for 10 searches?Python 内置 sorted() 使用 Timsort(最坏情况 O(n log n))。用 Timsort 重新计算方案 2(排序步骤数 = 1,000,000 x 20)。这是否改变了对 10 次搜索的建议? [2]
PART III  ·  MODELING / APPLIED第三部分  ·  建模与应用Universal / multi-region applied · 25 marks通用/多地区应用题 · 共 25 分

Section C · Modeling and ApplicationsC 部分 · 建模与应用

Read each scenario carefully. Where pseudocode is requested, use the standard block format. Where Python is requested, show correct indentation. Every question in this part is MEDIUM or HARD difficulty -- no EASY questions appear here.仔细阅读每个场景。伪代码使用标准块格式。Python 须正确缩进。本部分所有题目均为中等或高难度,无易题。

Q10 MEDIUM 🇺🇸 US 🇨🇦 ON AP CSP-feeder FRQAP CSP 衔接简答题 §1 + §2 Search selection + trace查找选择 + 追踪 · ICS4U A3.2 / AP CSP 3.11 [8 marks][8 分]

A library system stores book ID numbers. A librarian searches for book ID 47.图书馆系统存储图书 ID 号。馆员搜索图书 ID 47

Scenario A:场景 A: The IDs are stored as [23, 47, 8, 61, 15, 39, 72] (unsorted).ID 以 [23, 47, 8, 61, 15, 39, 72] 存储(无序)。

Scenario B:场景 B: The IDs are stored as [8, 15, 23, 39, 47, 61, 72] (sorted).ID 以 [8, 15, 23, 39, 47, 61, 72] 存储(已排序)。

(a) For Scenario A, state which search algorithm must be used and why. Trace it to find 47, listing each index checked.对于场景 A,说明必须使用哪种搜索算法及原因。追踪算法找到 47,列出每个检查的索引。 [3]
(b) For Scenario B, trace binary search step by step, completing a table with low, high, mid, and data[mid] for each step.对于场景 B,逐步追踪二分查找,填写每步的 lowhighmiddata[mid] 表格。 [3]
Step步骤lowhighmiddata[mid]Action操作
1
2
3
(c) Compare the number of comparisons made in (a) vs (b). State one advantage and one disadvantage of binary search relative to linear search.比较 (a) 和 (b) 中的比较次数。说明二分查找相对于线性查找的一个优点和一个缺点。 [2]
Q11 MEDIUM 🇨🇦 ON 🇨🇦 BC ON Provincial-style安大略省考风格 §4 + §5 + §6 Sort comparison + choice排序比较与选择 · ICS4U C2.3 / CSE3110 1.6.5 [9 marks][9 分]

A student is sorting student scores. The following three lists represent different scenarios. For each scenario, state which of bubble sort, selection sort, or insertion sort is the most appropriate choice and justify your answer.一名学生正在对学生分数进行排序。以下三个列表代表不同场景。对每种场景,说明冒泡排序、选择排序或插入排序中哪种最合适,并为你的答案提供理由。

(a) Scenario 1: [88, 91, 85, 89, 92, 87, 90] -- a nearly-sorted list where only a few elements are slightly out of order. Which sort is best? Justify in two sentences.场景 1:[88, 91, 85, 89, 92, 87, 90] -- 近乎有序的列表,只有少数几个元素轻微乱序。哪种排序最好?用两句话说明理由。 [3]
(b) Scenario 2: [95, 90, 85, 80, 75, 70, 65] -- a fully reverse-sorted list. Of bubble sort and selection sort, which makes fewer swaps? Count the swaps for each and state your answer.场景 2:[95, 90, 85, 80, 75, 70, 65] -- 完全逆序的列表。冒泡排序和选择排序中,哪个交换次数更少?各自计算交换次数并给出答案。 [4]
(c) A student argues that insertion sort is always better than bubble sort because they both have the same worst-case Big-O. State one case where this is true and one case where the two perform identically in terms of swaps.一名学生认为插入排序总是优于冒泡排序,因为两者具有相同的最坏情况大O复杂度。举一种插入排序确实更好的情况,以及一种两者在交换次数方面表现相同的情况。 [2]
Q12 HARD 🇺🇸 US 🇨🇦 ON 🇨🇦 BC AP CSP-feeder FRQAP CSP 衔接简答题 §7 Built-in sort + combined search/sort内置排序 + 排序/查找组合 · CSTA 3B-AP-11 / ICS4U A3.2 [8 marks][8 分]

A program manages a list of student (name, score) tuples. A student writes the following Python code.一个程序管理学生(姓名,分数)元组的列表。一名学生编写以下 Python 代码。

students = [("Zara", 88), ("Alex", 95), ("Maya", 73), ("Ben", 88), ("Priya", 91)]

# Sort by score ascending
students.sort(key=lambda s: s[1])

# Search for score 88 using linear search
target_score = 88
for i in range(len(students)):
    if students[i][1] == target_score:
        print("Found:", students[i])
        break
(a) State the contents of students after the sort() call.写出 sort() 调用后 students 的内容。 [2]
(b) Trace the linear search loop. State which element is found and printed, and on which iteration (value of i) the loop exits via break.追踪线性查找循环。写出找到并打印的元素,以及循环在哪次迭代(i 值为多少时)通过 break 退出。 [2]
(c) Rewrite the sort line so the list is sorted by score descending, with students having the same score sorted by name ascending as a tiebreaker. Write only the modified sort() call.重写排序行,使列表按分数降序排列,分数相同时按姓名升序作为次要排序键。只写修改后的 sort() 调用。 [2]
(d) Explain the difference between students.sort(...) and sorted(students, ...). In this program, which is preferable if you need to keep the original unsorted list? Justify in one sentence.解释 students.sort(...)sorted(students, ...) 的区别。如果需要保留原始未排序列表,本程序中哪个更合适?用一句话说明理由。 [2]

🇺🇸 US CSTA / AP CSP美国 CSTA / AP CSP3A-AP-11 · 3B-AP-11 · 3.11 · 3.17
🇨🇦 Ontario安大略ICS4U A3.2 · A3.4 · C2.2 · C2.3
🇨🇦 British Columbia不列颠哥伦比亚CS 11 / AP CSP: searching, sorting, Big-O conceptsCS 11 / APCSP:查找、排序、大O概念
🇨🇦 Alberta阿尔伯塔CSE3110 outcomes 1.5.1, 1.5.2, 1.5.4, 1.6.1, 1.6.2, 1.6.3, 1.6.5CSE3110 结果 1.5.1、1.5.2、1.5.4、1.6.1、1.6.2、1.6.3、1.6.5

Full Syllabus Map in Study Guide: ../Study Guides/Unit_7_Searching_and_Sorting.html. CS has no AB standalone diploma exam; AB framing uses CSE3110 outcomes.完整大纲对照见学习指南:../Study Guides/Unit_7_Searching_and_Sorting.html。CS 无独立 AB 毕业考;AB 题使用 CSE3110 结果框架。