0% found this document useful (0 votes)
12 views

Heap Sort

Uploaded by

Richa Singh
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Heap Sort

Uploaded by

Richa Singh
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Explain heap Sort Algorithm

Heap Sort is a popular and efficient sorting algorithm in computer programming.


Heap sort works by visualizing the elements of the array as a special kind of
complete binary tree called a heap.
The general approach of heap sort is
1. From the given array ,build initial max heap
2. Interchange the root(Maximum) element with the last element
3. Use repetitive downward operation from root node to rebuild the heap of
size one less than the starting
4. Repeat steps 1 and 2 untill there are no more elements
Analysis of heap sort:
max_heapify has complexity O(logN), build_maxheap has complexity O(N) and
we run max_heapify N−1 times in heap_sort function, therefore complexity of
heap_sort function is O(NlogN)
After building max-heap, the elements in the array Arr will be:
Step 1: 8 is swapped with 5.
Step 2: 8 is disconnected from heap as 8 is in correct
position now and.
Step 3: Max-heap is created and 7 is swapped with
3.
Step 4: 7 is disconnected from heap.
Step 5: Max heap is created and 5 is swapped with
1.
Step 6: 5 is disconnected from heap.
Step 7: Max heap is created and 4 is swapped with
3.
Step 8: 4 is disconnected from heap.
Step 9: Max heap is created and 3 is swapped with
1.
Step 10: 3 is disconnected.
After all the steps, we will get a sorted array.
Time and Space Complexity of Heap Sorting in Data Structure
Best = Ω(n log(n))
Average = Θ(n log(n))
Worst = O(n log(n))
The space complexity of Heap Sort is O(1).

Similarly, there is a concept of Max Heap and Min Heap. Like trees
and arrays, there is another organized Data Structure called Heap
Data Structure. It is a complete binary tree that follows the rule that
all the root nodes are larger(for Max Heap) or smaller (for Min Heap)
than their child nodes. This process is called Heapify
Advantages and Disadvantages of using Heap Sort in Data
Structure
Advantages: Optimized performance, efficiency, and accuracy are
a few of the best qualities of this algorithm. The algorithm is also
highly consistent with very low memory usage. No extra memory
space is required to work, unlike the Merge Sort or recursive
Quick Sort.
Disadvantages: Heap Sort is considered unstable, expensive, and
not very efficient when working with highly complex data.
You might have come across Dijkstra’s algorithm
that finds the shortest path where Heap Sort is
implemented. Heap Sort in Data Structure is used
when the smallest (shortest) or highest (longest)
value is needed instantly. Other usages include
finding the order in statistics, dealing with priority
queues in Prim’s algorithm (also called the
minimum spanning tree) and Huffman encoding or
data compression.
Similarly, various operating systems use this
algorithm for jobs and process management as it’s
based on the priority queue.
To every type of sorting or searching algorithm,
advantages and disadvantages are always there.
With Heap Sorting algorithms, there are very few
disadvantages. There is no additional requirement
of memory space.
The other factor is time. It is found that the time
complexity is calculated as nlog(n), but the actual
Heap Sort is lesser than O(nlog(n)). The reason is
that reduction or extraction from the Heap Sort
reduces the size and takes much less time as the
process goes on.
Hence, Heap Sorting is considered one of the ”best”
sorting algorithms because of various reasons in the
world of Data Structure.
On which algorithm is heap sort based on?
a) Fibonacci heap
b) Binary tree
c) Priority queue
d) FIFO

In what time can a binary heap be built?


a) O(N)
b) O(N log N)
c) O(log N)
d) O(N2)
Consider the following heap after buildheap phase. What
will be its corresponding array?
Answer: c
Explanation: Heap sort is based on the algorithm of priority queue and it gives the
best sorting time.
Answer: a
Explanation: The basic strategy is to build a binary heap of N elements which takes
O(N) time.
Answer: d
Explanation: Constructing a max heap using the elements 97,53,59,26,41,58,31 will
cause the heap to look like that.
4. In heap sort, after deleting the last minimum element, the array will contain
elements in?
a) increasing sorting order
b) decreasing sorting order
c) tree inorder
d) tree preorder
Answer: b
Explanation: By logic, after deleting minimum element, the heap will contain
elements in decreasing sorting order. We can change this by altering the ordering
property.
How many arrays are required to perform deletion operation in a
heap?
a) 1
b) 2
c) 3
d) 4

Answer: b
Explanation: To perform deletion operation in a heap, we require 2 arrays and that
occupies extra memory space and hence increase in running time.

What is the time taken to perform a delete min operation?


a) O(N)
b) O(N log N)
c) O(log N)
d) O(N2)
Answer: c
Explanation: The time taken to perform a deletion of a minimum element is
mathematically found to be O( log N).
Heap sort is an extremely stable algorithm.
a) true
b) false
Answer: a
Explanation: Heap sort uses fewer comparisons than other sorting algorithms and
hence it is an extremely stable algorithm.

What is the time taken to copy elements to and from two arrays created for
deletion?
a) O(N)
b) O(N log N)
c) O(log N)
d) O(N2)
Answer: a
Explanation: The time taken to copy elements to and from the main array and
extra array is found to be O(N).

You might also like