|
@@ -23,23 +23,23 @@ def heapify(arr, n, i, dim):
|
|
|
heapify(arr, n, largest, dim)
|
|
|
|
|
|
# 堆排序
|
|
|
-def heap_sort(arr, topK=None, dim=1):
|
|
|
+def heap_sort(arr, topk=None, dim=1):
|
|
|
n = len(arr)
|
|
|
# 构造大顶堆,从非叶子节点开始倒序遍历,因此是l//2 -1 就是最后一个非叶子节点
|
|
|
for i in range(n//2-1, -1, -1):
|
|
|
heapify(arr, n, i, dim)
|
|
|
- # 若topK不为None,则进行设定
|
|
|
- topK = n if topK > n or topK is None else topK
|
|
|
+ # 若topk不为None,则进行设定
|
|
|
+ topk = n if topk > n or topk is None else topk
|
|
|
res_list = []
|
|
|
# 上面的循环完成了大顶堆的构造,那么就开始把根节点跟末尾节点交换,然后重新调整大顶堆
|
|
|
- for i in range(n-1, n-1-topK, -1):
|
|
|
+ for i in range(n-1, n-1-topk, -1):
|
|
|
res_list.append(arr[0])
|
|
|
arr[0] = arr[i]
|
|
|
# arr[i], arr[0] = arr[0], arr[i]
|
|
|
heapify(arr, i, 0, dim)
|
|
|
|
|
|
return res_list
|
|
|
- # return arr[n-topK:][::-1]
|
|
|
+ # return arr[n-topk:][::-1]
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
arr = [[0,12], [1,11], [2,13], [3,5], [4,6], [5,7]]
|