Data Structures Lab 13 - Heapsort
Data Structures Lab 13 - Heapsort
5 2 4 6 1 3
sorted unsorted
Insertion Sort
INSERTION-SORT
Alg.: INSERTION-SORT(A) 1 2 3 4 5 6 7 8
a1 a2 a3 a4 a5 a6 a7 a8
for j ← 2 to n
do key ← A[ j ] key
i←j-1
while i > 0 and A[i] > key
do A[i + 1] ← A[i]
i←i–1
A[i + 1] ← key
• Insertion sort – sorts the elements in place
Time Complexity
• Best case: If given elements are in sorted
order the complexity is (n)
• Worst case: (n2)
HEAPS
Heap is an application of Binary Tree.
Property : The root of max heap/min heap contains the largest/smallest key.
Heaps are frequently used to implement priority queues.
A max tree is a tree in which the key value in each node is larger than the key values in its
children.
A max heap is a complete binary tree that is also a max tree.
A min tree is a tree in which the key value in each node is smaller than the key values in its
children.
A min heap is a complete binary tree that is also a min tree.
Operations on heaps:
creation of an empty heap
insertion of a new element into the heap;
deletion of the largest element from the heap
Max Heap Examples:
• To determine the correct place for the element that is being inserted, we use a
bubbling up process that begins at the new node of the tree and moves towards the
root.
• The element to be inserted bubbles up as far as is necessary to ensure a max heap.
• Consider the max heap :
• Insert 5 : Initially adds 5 as the left child of 2, following the complete binary Tree
property
• to follow the max heap property the nodes 2 and 5 are swapped.
Insertion into a Max Heap
• Insert 21 : Following the complete binary tree property, the node 21 is initially inserted
as left child of node 2
• Which, violates the max heap property, therefore nodes 2 and 21 are swapped
• It still violates the max heap property, therefore nodes 20 and 21 are swapped.
Deletion from a Max Heap
• When an element is to be deleted from a max heap, it is taken from the root of the
heap.
.
Find the largest element in the left and right children of root, Swap its position with the
node 2.
• Structure of Heap:
Example for Building max heap
• Consider the elements: 6 5 3 1 8 7 2 4
• The above array represented as a complete binary tree would be:
Tree-1
Heap Sort
• Heap sort is a comparison based sorting technique based on Binary Heap data structure.
• It is similar to selection sort where we first find the maximum element and place the
maximum element at the end.
• We repeat the same process for remaining elements.
• It is an in-place algorithm (the same array is used for all the steps), but not a stable sort.
• A sorting algorithm is said to be stable if two objects with equal keys appear in the same
order in sorted output as they appear in the input array to be sorted.
ALGORITHM
1. Build a max heap from the input data. (Sort in ascending order)
2. At this point, the largest item is stored at the root of the heap. Swap it with the
last item of the heap followed by reducing the size of heap by 1.
3. Finally, heapify the root of tree (i.e build a max heap from remaining elements)
4. Repeat above steps while size of heap is greater than 1.
Heap Sort & Heapify Algorithms
Heap Sort Algorithm:
void heapSort(int arr[], int n)
{
for (int i = n / 2 - 1; i >= 0; i--) // Build heap (rearrange array)
heapify(arr, n, i);
for (int i=n-1; i>=0; i--) // One by one extract an element from heap
{
swap(&arr[0], &arr[i]); // Move current root to end
heapify(arr, i, 0); // call max heapify on the reduced heap
}
}
Heapify Algorithm:
void heapify(int arr[], int n, int i)
{
int largest = i; // Initialize largest as root
int l = 2*i + 1; // left = 2*i + 1
int r = 2*i + 2; // right = 2*i + 2
if (l < n && arr[l] > arr[largest]) // If left child is larger than root
largest = l;
if (r < n && arr[r] > arr[largest]) // If right child is larger than largest so far
largest = r;