4.1 topk问题
"""
topk问题
获取一个大列表中值最大的前10个元素
"""
import random
from cal_time import cal_time
def sift(li, low, high):
"""
向下调整实现
:param li: 一个非堆结构的完全二叉树,其根结点的左右子树都是堆结构
:param low: 堆的根节点位置
:param high: 堆的最后一个元素的位置
:return:
"""
i = low
j = 2 * i + 1
tmp = li[low]
while j <= high:
if j + 1 <= high and li[j + 1] < li[j]:
j = j + 1
if li[j] < tmp:
li[i] = li[j]
i = j
j = 2 * i + 1
else:
li[i] = tmp
break
else:
li[i] = tmp
@cal_time
def topk(li, k):
"""
获取列表内最大的10个数
:param li:
:param k:
:return:
"""
heap = li[0:k]
for i in range((k - 2) // 2, -1, -1):
sift(heap, i, k - 1)
for i in range(k, len(li) - 1):
if li[i] > heap[0]:
heap[0] = li[i]
sift(heap, 0, k - 1)
for i in range(k - 1, -1, -1):
heap[0], heap[i] = heap[i], heap[0]
sift(heap, 0, i - 1)
return heap
li_10 = list(range(100000))
random.shuffle(li_10)
print(topk(li_10, 10))