Wednesday, October 15, 2008

问个算法题(zz)

发信人: erniu (山顶洞人), 信区: JobHunting
标 题: 问个算法题
发信站: BBS 未名空间站 (Wed Oct 15 01:48:53 2008)

经常看到的一个问题是从N个数中找出最出最大/小的K个数.有人用Heap,有人用
Quicksort的变化.如果问题变为找最大/小的2个数,什么方法最优?



发信人: yoyojade (NewCop), 信区: JobHunting
标 题: Re: 问个算法题
发信站: BBS 未名空间站 (Wed Oct 15 08:34:41 2008)

this depends on,

1. if you have enough memory, save all data in the memory, you can use quick
select find the k biggest/smallest in O(n) time

2. If you don't have enough memory to save all data, use heap of size k, you
can find the biggest/smallest k number in O(nlogk)



发信人: businessp (bp), 信区: JobHunting
标 题: Re: 问个算法题
发信站: BBS 未名空间站 (Wed Oct 15 10:25:34 2008)

同时找到最大(小)和第二大(小)的数是floor(3/2n),CLRS上有讲解吧
--

No comments: