We have all the methods we need in order to create our
heapsort() function does the following:
- Creates an empty list that will hold our sorted values.
- Creates an instance of
- Adds values from our parameter list into our
In order to complete the function, here’s what we need to do:
- Create a
whileloop that iterates while our heap has at least one element.
- Inside the loop, extract the largest value from the heap and place it at the beginning of the sorted list. Then, re-heapify the heap to ensure it maintains its heap properties.
- Return the sorted list.
Tab over to script.py.
heapsort() function, create a
while loop that continues to execute while
max_heap.count is greater than
while loop, do the following:
- Create a variable called
max_valueand set it to the largest value in the heap.
max_valueto the beginning of
Finally, outside the
while loop, return
Note: Since we added a print statement to the end of our program, the code should output a sorted list!