Top MCQs on HeapSort Algorithm with Answers

Last Updated : 27 Sep, 2023

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 element … More on Heap Sort

Heap Sort

Heap Sort


Question 1

Which of the following sorting algorithms in its typical implementation gives best performance when applied on an array which is sorted or almost sorted (maximum 1 or two elements are misplaced).

Cross

Quick Sort

Cross

Heap Sort

Cross

Merge Sort

Tick

Insertion Sort



Question 1-Explanation: 

Insertion sort takes linear time when input array is sorted or almost sorted (maximum 1 or 2 elements are misplaced). All other sorting algorithms mentioned above will take more than linear time in their typical implementation.

Question 2

Given an unsorted array. The array has this property that every element in the array is at most k distance from its position in a sorted array where k is a positive integer smaller than the size of an array. Which sorting algorithm can be easily modified for sorting this array and what is the obtainable time complexity?

Cross

Insertion Sort with time complexity O(kn)

Tick

Heap Sort with time complexity O(nLogk)

Cross

Quick Sort with time complexity O(kLogk)

Cross

Merge Sort with time complexity O(kLogk)



Question 2-Explanation: 

We can perform this in O(nlogK) time using heaps:

First, create a min-heap with first k+1 elements. Now, we are sure that the smallest element will be in this K+1 element. Now, remove the smallest element from the min-heap(which is the root) and put it in the result array. Next, insert another element from the unsorted array into the mean-heap, now, the second smallest element will be in this..extract it from the mean-heap and continue this until no more elements are in the unsorted array. Next, use a simple heap sort for the remaining elements.

Time Complexity:

O(k) to build the initial min-heap
O((n-k)logk) for remaining elements.

Thus we get O(nlogk). Hence, option B is correct.

Question 3

Which of the following is not true about comparison-based sorting algorithms?

Cross

The minimum possible time complexity of a comparison-based sorting algorithm is O(n(log(n)) for a random input array

Cross

Any comparison based sorting algorithm can be made stable by using position as a criteria when two elements are compared

Cross

Counting Sort is not a comparison based sorting algorithm

Tick

Heap Sort is not a comparison based sorting algorithm.



Question 3-Explanation: 

Heap Sort is not a comparison based sorting algorithm is not correct.

Question 4

Suppose we are sorting an array of eight integers using heapsort, and we have just finished some heapify (either maxheapify or minheapify) operations. The array now looks like this: 16 14 15 10 12 27 28 How many heapify operations have been performed on root of heap?

Cross

1

Tick

2

Cross

3 or 4

Cross

5 or 6



Question 4-Explanation: 

In Heapsort, we first build a heap, then we do following operations till the heap size becomes 1. a) Swap the root with last element b) Call heapify for root c) reduce the heap size by 1. In this question, it is given that heapify has been called few times and we see that last two elements in given array are the 2 maximum elements in array. So situation is clear, it is maxheapify which has been called 2 times.

Hence Option(B) is the correct answer.

Question 5
You have to sort 1 GB of data with only 100 MB of available main memory. Which sorting technique will be most appropriate?
Cross
Heap sort
Tick
Merge sort
Cross
Quick sort
Cross
Insertion sort


Question 5-Explanation: 
The data can be sorted using external sorting which uses merging technique. This can be done as follows: 1. Divide the data into 10 groups each of size 100. 2. Sort each group and write them to disk. 3. Load 10 items from each group into main memory. 4. Output the smallest item from the main memory to disk. Load the next item from the group whose item was chosen. 5. Loop step #4 until all items are not outputted. The step 3-5 is called as merging technique.
Question 6
Which sorting algorithms is most efficient to sort string consisting of ASCII characters?
Cross
Quick sort
Cross
Heap sort
Cross
Merge sort
Tick
Counting sort


Question 6-Explanation: 
Counting sort algorithm is efficient when range of data to be sorted is fixed. In the above question, the range is from 0 to 255(ASCII range). Counting sort uses an extra constant space proportional to range of data.
Question 7
The number of elements that can be sorted in \\Theta(logn) time using heap sort is
(A) \\Theta(1)
(B) \\Theta(\\sqrt{logn})
(C) \\Theta(Log n/(Log Log n))
(d) \\Theta(Log n) 
Cross
A
Cross
B
Tick
C
Cross
D


Question 7-Explanation: 
Time complexity of Heap Sort is \\Theta(mLogm) for m input elements. For m = \\Theta(Log n/(Log Log n)), the value of \\Theta(m * Logm) will be \\Theta( [Log n/(Log Log n)] * [Log (Log n/(Log Log n))] ) which will be \\Theta( [Log n/(Log Log n)] * [ Log Log n - Log Log Log n] ) which is \\Theta(Log n)
Question 8

Which of the following is true about merge sort?

Cross

Merge Sort works better than quick sort if data is accessed from slow sequential memory.

Cross

Merge Sort is stable sort by nature

Cross

Merge sort outperforms heap sort in most of the practical situations.

Tick

All of the above.



Question 8-Explanation: 

Merge Sort satisfies all the three options given above.

Question 9
Consider a binary min heap containing n elements and every node is having degree 2 ( i.e. full binary min heap tree). What is the probability of finding the largest element at the last level ?
Cross
1/2
Tick
1
Cross
1/n
Cross
1/2^n


Question 9-Explanation: 
Always 1 as maximum element will always be present in the leaf nodes in case of binary min heap.
There are 9 questions to complete.

 

Coding practice for sorting.

 


Share your thoughts in the comments

Similar Reads