求助一道算法题

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该怎样去找,等待高人指点!
...全文
136 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
找出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
  • 打赏
  • 举报
回复
没人会做吗?

33,006

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧