Sorting Algorithms — Algorithms 练习题
Sorting arranges elements in order (ascending or descending). Different algorithms trade off simplicity, speed, and memory. Stability means equal elements keep their relative order after sorting.
Bubble sort repeatedly swaps adjacent out-of-order pairs — O(n²) time, simple but slow. Selection sort finds minimum and swaps — also O(n²). Merge sort divides, sorts halves, merges — O(n log n), stable, uses extra memory.
Quicksort picks a pivot, partitions smaller/larger — average O(n log n), in-place variants common. For small or nearly sorted data, simpler algorithms may suffice; for large data, O(n log n) is preferred.
练习的技能
- Tracing simple sorts on small arrays
- Comparing time complexities
- Understanding stable vs. unstable sorts
- Choosing appropriate sorting algorithm
练习题:Sorting Algorithms
说明: 仔细解答每一道题。清晰地展示所有解题过程。在指定位置或按指示在另一张纸上写出最终答案。
-
1.**Multiple Choice:** Which of the following sorting algorithms has the best average-case time complexity?
- A. Bubble Sort
- B. Insertion Sort
- C. Merge Sort
- D. Selection Sort
Sorting Algorithms — 练习题(续)
-
2.**Short Answer:** What is the worst-case time complexity of Quick Sort, and what condition causes it?
-
3.**Free Response:** Trace the execution of Merge Sort on the following array: . Show each recursive split and the merge steps.
Sorting Algorithms — 练习题(续)
-
4.**Multiple Choice:** Which sorting algorithm works by repeatedly swapping adjacent elements if they are in the wrong order?
- A. Merge Sort
- B. Bubble Sort
- C. Quick Sort
- D. Heap Sort
Sorting Algorithms — 练习题(续)
-
5.**Short Answer:** Explain why Insertion Sort is efficient for nearly sorted data. What is its best-case time complexity?
Sorting Algorithms — 练习题(续)
-
6.**Free Response:** Perform one complete pass of Bubble Sort on the array , showing the array after each swap. Then state whether the array is fully sorted after that pass.
Sorting Algorithms — 练习题(续)
-
7.**Multiple Choice:** Which of the following is **not** an in-place sorting algorithm?
- A. Quick Sort
- B. Heap Sort
- C. Merge Sort
- D. Insertion Sort
Sorting Algorithms — 练习题(续)
-
8.**Short Answer:** What is the key property of a heap that makes Heap Sort possible? Briefly describe how Heap Sort works.
Sorting Algorithms — 练习题(续)
-
9.**Free Response:** Given the array , show the result after each pass of Selection Sort. How many passes are needed to fully sort the array?
Sorting Algorithms — 练习题(续)
-
10.**Multiple Choice:** Which sorting algorithm has a worst-case time complexity of but an average-case of ?
- A. Merge Sort
- B. Quick Sort
- C. Heap Sort
- D. Bubble Sort
参考答案
-
1.
Merge Sort has average-case ; Bubble, Insertion, and Selection are .最终答案: C
-
2.
最终答案: Worst-case time complexity is . This occurs when the pivot chosen is always the smallest or largest element (e.g., already sorted or reverse-sorted array with poor pivot selection).
-
3.
最终答案: Split: and ; Further splits until single elements: ; , , , ; Merge ; ; ; Merge and ; Merge and ; Final merge: and
-
4.
Bubble Sort repeatedly swaps adjacent elements if they are out of order.最终答案: B
参考答案(续)
-
5.
最终答案: Insertion Sort is efficient for nearly sorted data because it only makes a few comparisons and shifts; best-case time complexity is (already sorted).
-
6.
最终答案: Pass 1: ; Compare 5 and 1: swap ; Compare 5 and 4: swap ; Compare 5 and 2: swap ; Compare 5 and 8: no swap ; Array after pass: . Not fully sorted (4 and 2 are out of order).
-
7.
Merge Sort requires extra space, so it is not in-place.最终答案: C
-
8.
最终答案: A heap is a complete binary tree where each parent is greater (max-heap) or smaller (min-heap) than its children. Heap Sort builds a max-heap, then repeatedly swaps the root with the last element and reheapifies the reduced heap.
参考答案(续)
-
9.
最终答案: Pass 1: ; Pass 2: ; Pass 3: ; Pass 4: ; Pass 5: (no change) ; 6 passes are needed (for , Selection Sort requires passes).
-
10.
Quick Sort has worst-case but average-case .最终答案: B
需要避免的常见错误
- Assuming bubble sort is O(n) with early exit always
- Confusing merge sort space complexity
- Incorrect pivot partition in quicksort traces
- Thinking all O(n²) sorts behave identically in practice
生成您自己的练习表
想要一套难度自选的 sorting algorithms 新题?使用 WorksheetSmith 在几秒内编译一份可打印的自定义 PDF。
最后更新:2026