堆排序解决 top k 问题

来源:互联网 发布:linux zip 打包文件夹 编辑:程序博客网 时间:2024/06/06 18:21
import timeimport heapqimport randomdef gen_large_arr(n):    arr = [i for i in range(n)]    random.shuffle(arr)    return arr# find top n item from bigarray using liner searchdef liner_search(bigarray, n):    return sorted(bigarray, reverse=True)[:n]# using heap sortdef heap_search(bigarray, n):    heap = []    for elem in bigarray:        if len(heap) < n or elem > heap[0]:            if len(heap) == n:                heapq.heappop(heap)            heapq.heappush(heap, elem)    return heapstart = time.time()bigarray = gen_large_arr(10 * 1000 * 1000)print 'gen arr took %g s' % (time.time() - start)start = time.time()print liner_search(bigarray, 10)print 'linear search took %g s' % (time.time() - start)start = time.time()print heap_search(bigarray, 10)print 'heap search took %g s' %(time.time() - start)
0 0
原创粉丝点击