# HeapSort

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

Quick Sort | |

Heap Sort | |

Merge Sort | |

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?

Insertion Sort with time complexity O(kn) | |

Heap Sort with time complexity O(nLogk) | |

Quick Sort with time complexity O(kLogk) | |

Merge Sort with time complexity O(kLogk) |

**Analysis of Algorithms**

**Sorting**

**QuickSort**

**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?

The minimum possible time complexity of a comparison based sorting algorithm is O(nLogn) for a random input array | |

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

Counting Sort is not a comparison based sorting algortihm | |

Heap Sort is not a comparison based sorting algorithm. |

**Analysis of Algorithms**

**Sorting**

**HeapSort**

**CountingSort**

**Discuss it**

Question 3 Explanation:

See http://www.geeksforgeeks.org/lower-bound-on-comparison-based-sorting-algorithms/ for point A. See http://www.geeksforgeeks.org/stability-in-sorting-algorithms/ for B. C is true, count sort is an Integer Sorting algorithm.

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?

1 | |

2 | |

3 or 4 | |

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?

Heap sort | |

Merge sort | |

Quick sort | |

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?

Quick sort | |

Heap sort | |

Merge sort | |

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 time using heap sort is

(A) (B) (C) (d)

A | |

B | |

C | |

D |

**Analysis of Algorithms**

**Sorting**

**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?

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

Merge Sort is stable sort by nature | |

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

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 ?

1/2 | |

1 | |

1/n | |

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.

## Coding practice for sorting.