MCPcopy
hub / github.com/Jack-Lee-Hiter/AlgorithmsByPython / heapSort

Function heapSort

heapSort.py:1–23  ·  view source on GitHub ↗
(alist)

Source from the content-addressed store, hash-verified

1def heapSort(alist):
2 if alist == None or len(alist) == 0:
3 return
4 length = len(alist)
5 output = []
6 for i in range(length):
7 tempLen = len(alist)
8 for j in range(tempLen//2-1, -1, -1):
9 preIndex = j
10 preVal, heap = alist[preIndex], False
11 while 2 * preIndex < tempLen - 1 and not heap:
12 curIndex = 2 * preIndex + 1
13 if curIndex < tempLen - 1:
14 if alist[curIndex] < alist[curIndex+1]:
15 curIndex += 1
16 if preVal >= alist[curIndex]:
17 heap = True
18 else:
19 alist[preIndex] = alist[curIndex]
20 preIndex = curIndex
21 alist[preIndex] = preVal
22 output.insert(0, alist.pop(0))
23 return output
24
25test = [2, 6, 5, 9, 10, 3, 7]
26print(heapSort(test))

Callers 1

heapSort.pyFile · 0.85

Calls 2

insertMethod · 0.80
popMethod · 0.45

Tested by

no test coverage detected