我有這個python heapsort-code由僞代碼製作而成。爲什麼我的heapsort不工作?
但它給出了錯誤的結果。
def heapSortUp(a):
heapifyUp(a, len(a))
end = len(a)-1
while end > 0:
a[end], a[0] = a[0], a[end]
end -= 1
siftUp(a, 0, end)
return a
def heapifyUp(a, count):
end = 1
while end < count:
siftUp(a, 0, end)
end += 1
def siftUp(a, start, end):
child = end
while child > start:
parent = int(math.floor((child-1)/2)) # floor = abrunden
if a[parent] < a[child]:
a[parent], a[child] = a[child], a[parent]
child = parent
else:
return
我特別想使用siftUP版本。
通過計算print heapSortUp([1,5,4,2,9,8,7])
返回:[8, 7, 9, 2, 1, 4, 5, 7, 5]
@hammar我固定它用於堆排序 –