← 回題庫

卷別

資料結構與演算法

選擇

國立臺灣大學 2024 · 考試 · 第 1 題

考科演算法

(5 points) Consider the following variation of the Selection Problem: Given a sequence S=⟨a1,a2,…,an⟩S = \langle a_1, a_2, \dots, a_n \rangle of nn distinct integers and two positive integers xx and yy, where each number in SS falls between 1 and yy, the goal is to report the smallest xx numbers in SS in ascending order. This scenario assumes the conventional single-processor, random access machine (RAM) model, with a machine word size of at least ⌈log⁡y⌉\lceil \log y \rceil bits. To tackle this problem, five algorithms have been proposed. Please select the correct description(s) below.

  • (A)
    Algorithm A: The algorithm initializes an empty max-heap and sequentially inserts numbers from SS. During insertion, if the size of the max-heap exceeds xx, it removes the maximum element from the heap. Finally, the remaining numbers in the heap are sorted by heapsort and reported. If x≤n/log⁡nx \le n/\log n, Algorithm A runs in O(n)O(n) time in the worst case.
  • (B)
    Algorithm B: The algorithm initially constructs a min-heap and proceeds to extract the minimum element xx times. If x≤n/log⁡nx \le n/\log n, Algorithm B runs in O(n)O(n) time in the worst case.
  • (C)
    Algorithm C: The algorithm begins by performing a base-nn radix sort, followed by reporting the smallest xx values. If y≤n2y \le n^2, Algorithm C runs in O(n)O(n) time in the worst case.
  • (D)
    Algorithm D: The algorithm performs a counting sort by allocating an array CC of length yy, traversing through SS and incrementing C[ai]C[a_i] for each aia_i. It finally reports the smallest xx values by scanning through the counter array. If y≤n2y \le n^2, Algorithm D runs in O(n)O(n) time in the worst case.
  • (E)
    Algorithm E: The algorithm first applies quickselect, i.e., Randomized-Select, to determine the xx-th smallest number in SS. Subsequently, it traverses SS for extracting all numbers smaller than or equal to the xx-th smallest number. Finally, these xx numbers are sorted by quicksort and reported. The time complexity of Algorithm E is O(n+xlog⁡x)O(n + x \log x) in expectation.

標準解析

AI 已複查