求助一道算法题

sdnd2000 2008-04-17 09:13:04
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 finding 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 find the MAX
in each iteration of step 2.2.
Hint: You can find a reasonable upper bound by observing that each subsequent iteration takes
no more than the first iteration.

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

我实在不明白key-comparison该怎样去找,等待高人指点!
...全文
118 点赞 收藏 6
写回复
6 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
找出p个数中的最大值,p个数肯定都要遍历到。如果单纯看比较次数,说p-1次是要更准确一些。
回复
sdnd2000 2008-04-20
我再问下,第二问为什么不是p-1次.
回复
sdnd2000 2008-04-19
谢谢!
回复
sdnd2000 2008-04-19
没人知道么,顶一下!
回复
“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个元素)采用的都是插入排序的方法,这里问的是最坏情况下的比较次数。
当数据按大小顺序来的时候最糟糕,需要最多的比较次数才能确定插入位置。这时候:
如果采用顺序查找,最多的比较次数为1+2+…+(p-1)=p*(p-1)/2;
如果采用折半查找,最多的比较次数为lg1+lg2+…+lg(p-1)=lg((p-1)!)
(b)找出P个元素中最大的,线性时间就够了,2.2的比较次数是P
(c)综上,整个算法的复杂度达到了O(P^3)=O(n*sqrt(n)),比一般O(n^2)的排序算法稍好一些。

建议楼主别急着做题,先好好看看书吧,或者换本简单的、基础点的教材。
回复
sdnd2000 2008-04-18
没人会做吗?
回复
相关推荐
发帖
数据结构与算法
创建于2007-08-27

3.2w+

社区成员

数据结构与算法相关内容讨论专区
申请成为版主
帖子事件
创建了帖子
2008-04-17 09:13
社区公告
暂无公告