# 求助一道算法题

3. This problem is to analyze the worst-case number of key comparisons for a simple 2-level sorting algorithm described below. Let n = p2 be the number of elements to be sorted.

2.0 Divide the elements into p sets, each of size p.

2.1 Sort the elements within each set using insertion sort.

2.2 Let M[i] be the largest element in the sorted set i. Find the maximum value of all
M[i], 1 ≤ i ≤ p. Output this MAX element, while deleting it from its set.

2.3 Repeat step 2.2 in a similar fashion for ﬁnding the second MAX, third MAX, etc.

(a) What is the worst-case number of key comparisons to sort each set of p elements in step 2.1?
Explain.

(b) Give a reasonable upper bound for the worst-case number of key comparisons to ﬁnd the MAX
in each iteration of step 2.2.
Hint: You can ﬁnd a reasonable upper bound by observing that each subsequent iteration takes
no more than the ﬁrst iteration.

(c) Use (a) and (b) to ﬁnd a reasonable upper bound for the worst-case number of key comparisons
(in terms of n) for the entire algorithm.

...全文
118 点赞 收藏 6

6 条回复

sdnd2000 2008-04-20

“Let n = p2 be the number of elements to be sorted”
=> “Let n = p^2 be the number of elements to be sorted”

(a)对每个小集合（每个集合中都有p个元素）采用的都是插入排序的方法，这里问的是最坏情况下的比较次数。

(b)找出P个元素中最大的，线性时间就够了，2.2的比较次数是P
(c)综上，整个算法的复杂度达到了O(P^3)=O(n*sqrt(n))，比一般O(n^2)的排序算法稍好一些。

3.2w+