(alist)
| 1 | def 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 | |
| 25 | test = [2, 6, 5, 9, 10, 3, 7] |
| 26 | print(heapSort(test)) |
no test coverage detected