Engineering Questions with Answers - Multiple Choice Questions

Home » MCQs » Aeronautical Engineering » Heapsort Multiple Choice MCQ – 2

# Heapsort Multiple Choice MCQ – 2

Heap sort is an implementation of ____________ using a descending priority queue.

a) insertion sort

b) selection sort

c) bubble sort

d) merge sort

**
View Answer**

Answer: b

Explanation: Heap sort is an implementation of selection sort using the input array as a heap representing a descending priority queue. Heap sort algorithm is divided into two phase. In first phase the max-heap is created and the second phase (selection phase) deletes the elements from the priority queue using siftdown operation.

Which one of the following is false?

a) Heap sort is an in-place algorithm

b) Heap sort has O(nlogn) average case time complexity

c) Heap sort is stable sort

d) Heap sort is a comparison-based sorting algorithm

**
View Answer**

Answer: c

Explanation: Heap sort is a comparison based sorting algorithm and has time complexity O(nlogn) in the average case. Heap sort is an in-place algorithm as it needs O(1) of auxiliary space. Heap sort uses heap and operations on heap can change the relative order of items with the same key values. Therefore, Heap sort is not a stable sort.

The essential part of Heap sort is construction of max-heap. Consider the tree shown below, the node 24 violates the max-heap property. Once heapify procedure is applied to it, which position will it be in?

a) 4

b) 5

c) 8

d) 9

The descending heap property is ___________

a) A[Parent(i)] = A[i]

b) A[Parent(i)] <= A[i] c) A[Parent(i)] >= A[i]

d) A[Parent(i)] > 2 * A[i]

**
View Answer**

Answer: c

Explanation: The max-heap is also known as descending heap. Max-heap of size n is an almost complete binary tree of n nodes such that the element at each node is less than or equal to the element at its parent node.

What is its wort case time complexity of Heap sort?

a) O(nlogn)

b) O(n^{2}logn)

c) O(n^{2})

d) O(n^{3})

**
View Answer**

Answer: a

Explanation: In Heap sort, the call to procedure build_Max-heap takes O(n) time and each of O(n) calls to the function max_Heapify takes O(logn) time. So the worst case complexity of Heap sort is O(nlogn).

In average case Heap sort is as efficient as the Quick sort.

a) True

b) False

**
View Answer**

Answer: b

Explanation: Quick sort is more efficient than Heap sort because experiments indicate that Heap sort requires twice as much time as Quick sort for randomly sorted input.

Choose the correct option to fill? X so that the code given below implements the Heap sort.

#include <stdio.h> void heapify(int arr[], int n, int i) { int largest = i; // Initialize largest as root int l = 2*i + 1; // left = 2*i + 1 int r = 2*i + 2; // right = 2*i + 2 if (l < n && arr[l] > arr[largest]) largest = l; if (r < n && arr[r] > arr[largest]) largest = r; if (largest != i) { swap(arr[i], arr[largest]); heapify(arr, n, largest); } } void heapSort(int arr[], int n) { for (int i = n / 2 - 1; i >= 0; i--) heapify(arr, n, i); for (int i=n-1; i>=0; i--) { X; heapify(arr, i, 0); } } void printArray(int arr[], int n) { for (int i=0; i<n; ++i) printf(“%d”,arr[i]); printf(“\n”); } int main() { int arr[] = {12, 11, 13, 5, 6, 7}; int n = sizeof(arr)/sizeof(arr[0]); heapSort(arr, n); printf(“Sorted array is \n"); printArray(arr, n); }

a) swap(arr[0], arr[n])

b) swap(arr[i], arr[n])

c) swap(arr[0], arr[i])

d) swap(arr[i], arr[2*i])

**
View Answer**

Answer: c

Explanation: Steps in heap sort are : (i) Build the max-heap, (ii) Swap the root element with the last element of the heap, (iii) Reduce the size of heap by 1 and heapify the root element, (iv) Repeat the steps form step number (v) until all the elements are sorted. Therefore the correct option is swap(arr[0], arr[i]).

Which one of the following is a variation of Heap sort?

a) Comb sort

b) Smooth sort

c) Binary tree sort

d) Shell sort

**
View Answer**

Answer: b

Explanation: Smooth sort is a variation of Heap sort. Smooth sort has O(nlogn) worst case time complexity like Heap sort. But Smooth sort takes O(n) time to sort the nearly sorted input array.

Introsort algorithm is combination of _____________

a) Quick sort and Heap sort

b) Quick sort and Shell sort

c) Heap sort and Merge sort

d) Heap sort and insertion sort

**
View Answer**

Answer: a

Explanation: Introsort is a hybrid sorting algorithm that combines Quick sort and Heap sort to retain advantages of both. It has worst case speed of Heap sort and average case speed of Quick sort.

How many elements can be sorted in O(logn) time using Heap sort?

a) O(1)

b) O(n/2)

c) O(logn/log(logn))

d) O(logn)

**
View Answer**

Answer: c

Explanation: The time complexity of Heap sort is O(klogk) for k input elements,

for k = logn/log(logn),

O(klogk) = O(logn/log(logn) * log(logn/log(logn)))

∴ O(klogk) = O(logn/log(logn) * (log(logn) – log(log(logn))))

= O(logn)

Hence the correct option is O(logn/log(logn)).