資料結構›Ch7 搜尋與排序
第 70 題/共 76 題
◀ DS 70/76
70. Selection Algorithm、Median of Medians
#DS-07-070易Selection AlgorithmMedian of Medians

Consider the prune-and-search algorithm using the median of medians for finding the kk-th smallest element in an unsorted array AA of nn distinct elements, where 1≤k≤n1 \le k \le n, as described below:

Algorithm Select(A, k): Input: An array A of n elements and an integer k, 1≤k≤∣A∣=n1 \le k \le |A| = n Output: The k-th smallest element in A Step 1: If ∣A∣≤5|A| \le 5, then sort array A (with any method, e.g., insertion sort) and return the element with rank k (i.e., the k-th element in the sorted array). Step 2: Divide the array A into groups of five elements each (ignore the last group if it has fewer than five elements). Step 3: Find the median of each group. Step 4: Recursively compute the median of the found medians, denoted by m. Step 5: Partition the original array into the following three sets: L={x∈A∣x<m}L = \{x \in A \mid x < m\} E={m}E = \{m\} R={x∈A∣x>m}R = \{x \in A \mid x > m\} Step 6: If k=∣L∣+1k = |L| + 1 then return mm Else if k≤∣L∣k \le |L| then return Select(L, k) Else return Select(R, k - |L| - 1).

Select one or more correct statements.

📄 中央115
跳轉到第題
▤完整推導請見《WH 資工筆記 · 資料結構》Ch7 搜尋與排序
本章題號 · 61–76 / 76