# HeapSort

• Last Updated : 28 Jun, 2021

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

 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    HeapSort    InsertionSort
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)
Sorting    QuickSort    Analysis of Algorithms    HeapSort
Discuss it

Question 2 Explanation:
1) to sort the array firstly create a min-heap with first k+1 elements and a separate array as resultant array. 2) because elements are at most k distance apart from original position so, it is guranteed that the smallest element will be in this K+1 elements. 3) remove the smallest element from the min-heap(extract min) and put it in the result array. 4) Now,insert another element from the unsorted array into the mean-heap, now,the second smallest element will be in this, perform extract min and continue this process until no more elements are in the unsorted array.finally, use simple heap sort for the remaining elements Time Complexity ------------------------ 1) O(k) to build the initial min-heap 2) O((n-k)logk) for remaining elements... 3) 0(1) for extract min so overall O(k) + O((n-k)logk) + 0(1) = O(nlogk)
 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 algorithm D Heap Sort is not a comparison based sorting algorithm.
Sorting    Analysis of Algorithms    CountingSort    HeapSort    50 Algorithms MCQs with Answers
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    HeapSort    Heap
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    CountingSort    HeapSort
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 time using heap sort is
(A)
(B)
(C)
(d)  
 A A B B C C D D
Sorting    Analysis of Algorithms    HeapSort
Discuss it

Question 7 Explanation:
Time complexity of Heap Sort is for m input elements. For m = , the value of will be which will be which is
 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
GATE 2017 Mock    HeapSort
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.

## Coding practice for sorting.

My Personal Notes arrow_drop_up