CmSc 250 Intro to Algorithms
Sorting Algorithms
Heapsort
Learning Goals
Exam-like questions
Based on Priority Queues
Sorts in O(NlogN) time by performing N times deleteMin operations.
Why O(NlogN)?
Basic algorithm (not very efficient - extra memory required):
Improvements: Use the same array to store the deleted
elements
instead
of using another array
How can we do this?
After each deletion we percolate down the hole that is
formed after the deletion,
and as a result we get a vacant
position in the array - the last cell.
There we store the deleted element.
When all the elements are deleted and stored in the same
array following the above method,
the elements will be
there in reversed order.
What is the remedy for this?
Complexity of heap sort: O(NlogN) is the rough estimate.
Here is an example of heapsort.
Learning Goals
Exam-like questions