We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent eceace8 commit a667d82Copy full SHA for a667d82
1 file changed
heapSort.py
@@ -5,23 +5,22 @@ def heapSort(alist):
5
output = []
6
for i in range(length):
7
tempLen = len(alist)
8
- for j in range((tempLen-1)//2, -1, -1):
9
- k = j
10
- v, heap = alist[k], False
11
- while not heap and 2*k <= tempLen-1:
12
- index = 2 * k
13
- if index < tempLen-1:
14
- if alist[index] < alist[index+1]:
15
- index += 1
16
- if v >= alist[index]:
+ for j in range(tempLen//2-1, -1, -1):
+ preIndex = j
+ preVal, heap = alist[preIndex], False
+ while 2 * preIndex <= tempLen - 1 and not heap:
+ curIndex = 2 * preIndex + 1
+ if curIndex < tempLen - 1:
+ if alist[curIndex] < alist[curIndex+1]:
+ curIndex += 1
+ if preVal >= alist[curIndex]:
17
heap = True
18
else:
19
- alist[k] = alist[index]
20
- k = index
21
- alist[k] = v
22
- heapVal = alist.pop(0)
23
- output.insert(0, heapVal)
+ alist[preIndex] = alist[curIndex]
+ preIndex = curIndex
+ alist[preIndex] = preVal
+ output.insert(0, alist.pop(0))
24
return output
25
26
test = [2, 6, 5, 9, 10, 3, 7]
27
-print(heapSort(test))
+print(heapSort(test))
0 commit comments