Skip to document
Was this document helpful?

Heap Sort Algorithm In Data Structures

Course: Btech (kcs-701)

478 Documents
Students shared 478 documents in this course
Was this document helpful?
Heap Sort Algorithm
In this article, we will discuss the Heapsort Algorithm. Heap sort processes
the elements by creating the min-heap or max-heap using the elements of
the given array. Min-heap or max-heap represents the ordering of array in
which the root element represents the minimum or maximum element of
the array.
Heap sort basically recursively performs two main operations -
oBuild a heap H, using the elements of array.
oRepeatedly delete the root element of the heap formed in 1st phase.
Before knowing more about the heap sort, let's first see a brief description
of Heap.
What is a heap?
A heap is a complete binary tree, and the binary tree is a tree in which the
node can have the utmost two children. A complete binary tree is a binary
tree in which all the levels except the last level, i.e., leaf node, should be
completely filled, and all the nodes should be left-justified.
33.4M
726
Exception Handling in Java - Javatpoint
What is heap sort?
Heapsort is a popular and efficient sorting algorithm. The concept of heap
sort is to eliminate the elements one by one from the heap part of the list,
and then insert them into the sorted part of the list.
Heapsort is the in-place sorting algorithm.
Now, let's see the algorithm of heap sort.
Algorithm
1. HeapSort(arr)
2. BuildMaxHeap(arr)
3. for i = length(arr) to 2
4. swap arr[1] with arr[i]
5. heap_size[arr] = heap_size[arr] ? 1
6. MaxHeapify(arr,1)