(nums)
| 21 | |
| 22 | |
| 23 | def heap_sort(nums): |
| 24 | n = len(nums) |
| 25 | |
| 26 | # Create a Max Heap from the list |
| 27 | # The 2nd argument of range means we stop at the element before -1 i.e. |
| 28 | # the first element of the list. |
| 29 | # The 3rd argument of range means we iterate backwards, reducing the count |
| 30 | # of i by 1 |
| 31 | for i in range(n, -1, -1): |
| 32 | heapify(nums, n, i) |
| 33 | |
| 34 | # Move the root of the max heap to the end of |
| 35 | for i in range(n - 1, 0, -1): |
| 36 | nums[i], nums[0] = nums[0], nums[i] |
| 37 | heapify(nums, i, 0) |
| 38 | |
| 39 | |
| 40 | # Verify it works |