DAA Assignment-02 28-11-2022
DAA Assignment-02 28-11-2022
Given an array of N elements. The task is to build a Binary Heap from the
given array. The heap can be either Max Heap or Min Heap .
Examples:
Input: arr[] = {4, 10, 3, 5, 1}
Output: Corresponding Max-Heap:
10
/ \
5 3
/ \
4 1
Heapify Illustration:
#include <stdio.h>
// Driver's Code
int main()
{
// Binary Tree Representation
// of input array
// 1
// / \
// 3 5
// / \ / \
// 4 6 13 10
// / \ / \
// 9 8 15 17
int arr[] = {1, 3, 5, 4, 6, 13, 10, 9, 8, 15, 17};
// Function call
buildHeap(arr, N);
printHeap(arr, N);
// Final Heap:
// 17
// / \
// 15 13
// / \ / \
// 9 6 5 10
// / \ / \
// 4 8 3 1
return 0;
}
Output
Array representation of Heap is:
17 15 13 9 6 5 10 4 8 3 1
Time Complexity: O(N)
Auxiliary Space: O(N)
What is Heap Sort
Heap sort is a comparison-based sorting technique based on Binary Heap data
structure. It is similar to the selection sort where we first find the minimum element
and place the minimum element at the beginning. Repeat the same process for the
remaining elements.
Heap sort is an in-place algorithm.
Its typical implementation is not stable, but can be made stable (See this)
Typically 2-3 times slower than well-implemented QuickSort. The reason
for slowness is a lack of locality of reference.
Advantages of heapsort:
Efficiency – The time required to perform Heap sort increases
logarithmically while other algorithms may grow exponentially slower as
the number of items to sort increases. This sorting algorithm is very
efficient.
Memory Usage – Memory usage is minimal because apart from what is
necessary to hold the initial list of items to be sorted, it needs no
additional memory space to work
Simplicity – It is simpler to understand than other equally efficient
sorting algorithms because it does not use advanced computer science
concepts such as recursion
Applications of HeapSort:
Heapsort is mainly used in hybrid algorithms like the IntroSort.
Sort a nearly sorted (or K sorted) array
k largest(or smallest) elements in an array
The heap sort algorithm has limited uses because Quicksort and Mergesort are better
in practice. Nevertheless, the Heap data structure itself is enormously used.
See Applications of Heap Data Structure
Recommended Problem
Heap Sort
Heap
Sorting
+1 more
Amazon
+7 more
Solve Problem
Transform into max heap: After that, the task is to construct a tree from that
unsorted array and try to convert it into max heap.
To transform a heap into a max-heap, the parent node should always be
greater than or equal to the child nodes
Here, in this example, as the parent node 4 is smaller than the
child node 10, thus, swap them to build a max-heap.
Transform it into a max heap image widget
Now, as seen, 4 as a parent is smaller than the child 5, thus swap both of
these again and the resulted heap and array should be like this:
Make the tree a max heap
Perform heap sort: Remove the maximum element in each step (i.e., move it to the
end position and remove that) and then consider the remaining elements and
transform it into a max heap.
Delete the root element (10) from the max heap. In order to delete this
node, try to swap it with the last node, i.e. (1). After removing the root
element, again heapify it to convert it into max heap.
Resulted heap and array should look like this:
Remove 10 and perform heapify
Repeat the above steps and it will look like the following:
Remove 5 and perform heapify
Now when the root is removed once again it is sorted. and the sorted
array will be like arr[] = {1, 3, 4, 5, 10}.
C
C++
Java
Python3
C#
PHP
Javascript
// Heap Sort in C
#include <stdio.h>
*a = *b;
*b = temp;
// n is size of heap
int largest = i;
// left = 2*i + 1
int left = 2 * i + 1;
// right = 2*i + 2
int right = 2 * i + 2;
largest = left;
// so far
largest = right;
if (largest != i) {
swap(&arr[i], &arr[largest]);
// sub-tree
heapify(arr, N, largest);
heapify(arr, N, i);
// Heap sort
swap(&arr[0], &arr[i]);
// root again
heapify(arr, i, 0);
printf("\n");
// Driver's code
int main()
// Function call
heapSort(arr, N);
printArray(arr, N);
Output
Sorted array is
5 6 7 11 12 13
Time Complexity: O(N log N)
Auxiliary Space: O(1)
Some FAQs related to Heap Sort
What are the two phases of Heap Sort?
The heap sort algorithm consists of two phases. In the first phase the array is
converted into a max heap. And in the second phase the highest element is removed
(i.e., the one at the tree root) and the remaining elements are used to create a new
max heap.
Why Heap Sort is not stable?
Heap sort algorithm is not a stable algorithm. This algorithm is not stable because
the operations that are performed in a heap can change the relative ordering of the
equivalent keys.
Is Heap Sort an example of “Divide and Conquer” algorithm?
Heap sort is NOT at all a Divide and Conquer algorithm. It uses a heap data
structure to efficiently sort its element and not a “divide and conquer approach” to
sort the elements.
Which sorting algorithm is better – Heap sort or Merge Sort?
The answer lies in the comparison of their time complexity and space requirement.
The Merge sort is slightly faster than the Heap sort. But on the other hand merge sort
takes extra memory. Depending on the requirement, one should choose which one to
use.
Why Heap sort better than Selection sort?
Heap sort is similar to selection sort, but with a better way to get the maximum
element. It takes advantage of the heap data structure to get the maximum element in
constant time.