(unsorted, index, heap_size)
| 14 | |
| 15 | |
| 16 | def heapify(unsorted, index, heap_size): |
| 17 | largest = index |
| 18 | left_index = 2 * index + 1 |
| 19 | right_index = 2 * index + 2 |
| 20 | if left_index < heap_size and unsorted[left_index] > unsorted[largest]: |
| 21 | largest = left_index |
| 22 | |
| 23 | if right_index < heap_size and unsorted[right_index] > unsorted[largest]: |
| 24 | largest = right_index |
| 25 | |
| 26 | if largest != index: |
| 27 | unsorted[largest], unsorted[index] = unsorted[index], unsorted[largest] |
| 28 | heapify(unsorted, largest, heap_size) |
| 29 | |
| 30 | |
| 31 | def heap_sort(unsorted): |