4 of 6
Write a function heap_sort(nums) that sorts a list in ascending order using only heap operations.
Rules:
heapq.sort() or sorted()print(heap_sort([5, 3, 8, 1, 4, 2, 7])) # Output: [1, 2, 3, 4, 5, 7, 8] print(heap_sort([10, 1, 10, 1])) # Output: [1, 1, 10, 10] print(heap_sort([42])) # Output: [42]
Sort in descending order using a heap. (Hint: negate trick!)
print(heap_sort_desc([5, 3, 8, 1, 4])) # Output: [8, 5, 4, 3, 1]