Heap Algorithm
Heap Algorithm
1
Heap Sort Algorithm
Initially on receiving an unsorted list, the first step in heap sort is to create a
Heap data structure (Max-Heap or Min-Heap). Once heap is built, the first
element of the Heap is either largest or smallest (depending upon Max-Heap
or Min-Heap), so we put the first element of the heap in our array. Then we
again make heap using the remaining elements, to again pick the first
element of the heap and put it into the array. We keep on doing the same
repeatedly until we have the complete sorted list in our array.
In the below algorithm, initially heapsort() function is called, which
calls heapify() to build the heap.
2
Heap Sort Algorithm
3
Heap Sort Algorithm
Heapsort is slower than Quicksort by factor 3.6 and slower than Merge Sort
by factor 2.4 for randomly distributed input data. For sorted data, heapsort is
eight to nine times slower than quicksort and two times slower than Merge
Sort.
4
Heap Sort Algorithm
Merge Sort is also usually faster than Heapsort. Besides, unlike Heapsort,
Merge Sort is stable.
Heapsort has an advantage over Merge Sort in that it does not require
additional memory, while Merge Sort requires additional memory in the
order of O(n).
Summary
If you liked the article, feel free to share it using one of the share buttons at
the end. Would you like to be informed by email when I publish a new
article? Then use the following form to sign up for my newsletter.
5
Heap Sort Algorithm
Reference
https://www.happycoders.eu/algorithms/heapsort/
https://www.studytonight.com/data-structures/heap-sort