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
 

1

HeapSort

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).
A
Quick Sort
B
Heap Sort
C
Merge Sort
D
Insertion Sort
Sorting    QuickSort    InsertionSort    HeapSort    
Discuss it


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 lienear time in their typical implementation.
Question 2
Given an unsorted array. The array has this property that every element in array is at most k distance from its position in sorted array where k is a positive integer smaller than size of array. Which sorting algorithm can be easily modified for sorting this array and what is the obtainable time complexity?
A
Insertion Sort with time complexity O(kn)
B
Heap Sort with time complexity O(nLogk)
C
Quick Sort with time complexity O(kLogk)
D
Merge Sort with time complexity O(kLogk)
Analysis of Algorithms    Sorting    QuickSort    HeapSort    
Discuss it


Question 2 Explanation: 
See http://www.geeksforgeeks.org/nearly-sorted-algorithm/ for explanation and implementation.
Question 3
Which of the following is not true about comparison based sorting algorithms?
A
The minimum possible time complexity of a comparison based sorting algorithm is O(nLogn) for a random input array
B
Any comparison based sorting algorithm can be made stable by using position as a criteria when two elements are compared
C
Counting Sort is not a comparison based sorting algortihm
D
Heap Sort is not a comparison based sorting algorithm.
Analysis of Algorithms    Sorting    HeapSort    CountingSort    
Discuss it


Question 3 Explanation: 
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?
A
1
B
2
C
3 or 4
D
5 or 6
Sorting    Heap    HeapSort    
Discuss it


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 whic has been called 2 times.
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?
A
Heap sort
B
Merge sort
C
Quick sort
D
Insertion sort
Sorting    QuickSort    MergeSort    HeapSort    
Discuss it


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?
A
Quick sort
B
Heap sort
C
Merge sort
D
Counting sort
Sorting    QuickSort    HeapSort    CountingSort    
Discuss it


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) 
A
A
B
B
C
C
D
D
Analysis of Algorithms    Sorting    HeapSort    
Discuss it


Question 7 Explanation: 
Time complexity of Heap Sort is [Tex]\Theta(mLogm)[/Tex] for m input elements. For m = [Tex]\Theta(Log n/(Log Log n))[/Tex], the value of [Tex]\Theta(m * Logm)[/Tex] will be [Tex]\Theta( [Log n/(Log Log n)] * [Log (Log n/(Log Log n))] )[/Tex] which will be [Tex]\Theta( [Log n/(Log Log n)] * [ Log Log n - Log Log Log n] )[/Tex] which is [Tex]\Theta(Log n)[/Tex]
Question 8
Which of the following is true about merge sort?
A
Merge Sort works better than quick sort if data is accessed from slow sequential memory.
B
Merge Sort is stable sort by nature
C
Merge sort outperforms heap sort in most of the practical situations.
D
All of the above.
Sorting    QuickSort    MergeSort    HeapSort    
Discuss it


Question 8 Explanation: 
See Merge Sort and this.
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 ?
A
1/2
B
1
C
1/n
D
1/2^n
HeapSort    GATE 2017 Mock    
Discuss it


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.
1

 

Coding practice for sorting.

 


GATE CS Corner


See Placement Course for placement preparation, GATE Corner for GATE CS Preparation and Quiz Corner for all Quizzes on GeeksQuiz.