我想知道此代码的整体时间复杂性!下面代码排列数组
我使用堆函数对数组进行排序,
import heapq
arr =[-74, 0, -4, 20, 7, 1, -4700, 74, 21, 71000, 87, 400, 9]
print ("The array: ", arr)
print("")
heapArray = arr
length = len(arr)
heapq.heapify(heapArray)
sortedArray = []
for each in (arr[:length]):
sortedArray.append(heapq.heappop(heapArray))
arr = sortedArray
print ("The sorted array is:", arr)
此代码对数组进行排序!我只想知道整体复杂性吗?
i used heap function to sort the array
import heapq
arr =[-74, 0, -4, 20, 7, 1, -4700, 74, 21, 71000, 87, 400, 9]
print ("The array: ", arr)
print("")
heapArray = arr
length = len(arr)
heapq.heapify(heapArray)
sortedArray = []
for each in (arr[:length]):
sortedArray.append(heapq.heappop(heapArray))
arr = sortedArray
print ("The sorted array is:", arr)
this code sorts an array! i just want to know the overall complexity?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
堆排序是(n log n)算法。
使用
hapq.heapify()
将以线性时间为单位的数组;或者,o(n)。python docs 供参考。
Heap sort is a (n log n) algorithm.
Using
heapq.heapify()
will sort the array in linear time; or, O(n).Python Docs for reference.