使用Python和分治法
from random import random
target = [random() for i in range(10000)]
def topn(target, n):
if len(target) <= n:
return target
else:
part1 = target[0:len(target)/2]
part2 = target[len(target)/2:]
top2n = (topn(part1, n) + topn(part2, n))
top2n.sort()
return top2n[0:n]
if __name__ == '__main__':
top10 = topn(target, 10)
本文介绍了一种使用Python实现的分治法算法,该算法能够有效地从大量随机数中找出最大的N个数。通过将数据集分成两部分并递归处理的方式,实现了高效的排序与选择过程。
1068

被折叠的 条评论
为什么被折叠?



