Top MCQs on QuickSort Algorithm with Answers

Last Updated : 27 Sep, 2023

Like Merge Sort, QuickSort is a Divide and Conquer algorithm. It picks an element as pivot and partitions the given array around the picked pivot. There are many different versions of quickSort that pick pivot in different ways … More on Quick Sort

Quick sort

Quick sort

Question 1
What is recurrence for worst case of QuickSort and what is the time complexity in Worst case?
Cross
Recurrence is T(n) = T(n-2) + O(n) and time complexity is O(n^2)
Tick
Recurrence is T(n) = T(n-1) + O(n) and time complexity is O(n^2)
Cross
Recurrence is T(n) = 2T(n/2) + O(n) and time complexity is O(nLogn)
Cross
Recurrence is T(n) = T(n/10) + T(9n/10) + O(n) and time complexity is O(nLogn)


Question 1-Explanation: 
The worst case of QuickSort occurs when the picked pivot is always one of the corner elements in sorted array. In worst case, QuickSort recursively calls one subproblem with size 0 and other subproblem with size (n-1). So recurrence is T(n) = T(n-1) + T(0) + O(n) The above expression can be rewritten as T(n) = T(n-1) + O(n) void exchange(int *a, int *b) { int temp; temp = *a; *a = *b; *b = temp; } int partition(int arr[], int si, int ei) { int x = arr[ei]; int i = (si - 1); int j; for (j = si; j <= ei - 1; j++) { if(arr[j] <= x) { i++; exchange(&arr[i], &arr[j]); } } exchange (&arr[i + 1], &arr[ei]); return (i + 1); } /* Implementation of Quick Sort arr[] --> Array to be sorted si --> Starting index ei --> Ending index */ void quickSort(int arr[], int si, int ei) { int pi; /* Partitioning index */ if(si < ei) { pi = partition(arr, si, ei); quickSort(arr, si, pi - 1); quickSort(arr, pi + 1, ei); } } [/sourcecode]
Question 2

Suppose we have an O(n) time algorithm that finds the median of an unsorted array. Now consider a QuickSort implementation where we first find the median using the above algorithm, then use the median as a pivot. What will be the worst-case time complexity of this modified QuickSort?

Cross

O(n^2 Logn)

Cross

O(n^2)

Cross

O(n Logn Logn)

Tick

O(nLogn)



Question 2-Explanation: 

If we use the median as a pivot element, then the recurrence for all cases becomes T(n) = 2T(n/2) + O(n)

The above recurrence can be solved using Master method. It falls in case 2 of the master method.
So, the worst-case time complexity of this modified QuickSort is O(nLogn). 

Question 3

Which of the following is not a stable sorting algorithm in its typical implementation.

Cross

Insertion Sort

Cross

Merge Sort

Tick

Quick Sort

Cross

Bubble Sort



Question 3-Explanation: 

Quick Sort is not a stable sorting algorithm in its typical implementation.

Question 4

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 4-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 5

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 5-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 6
Suppose we are sorting an array of eight integers using quicksort, and we have just finished the first partitioning with the array looking like this:
2  5  1  7  9  12  11  10 

Which statement is correct?
Tick
The pivot could be either the 7 or the 9.
Cross
The pivot could be the 7, but it is not the 9
Cross
The pivot is not the 7, but it could be the 9
Cross
Neither the 7 nor the 9 is the pivot.


Question 6-Explanation: 
7 and 9 both are at their correct positions (as in a sorted array). Also, all elements on left of 7 and 9 are smaller than 7 and 9 respectively and on right are greater than 7 and 9 respectively.
Question 7
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 7-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 8

In quick sort, for sorting n elements, the (n/4)th smallest element is selected as a pivot using an O(n) time algorithm. What is the worst-case time complexity of the quick sort?

(A) \\theta (n)

(B) \\theta (n*log(n))

(C) \\theta (n2)

(D) \\theta (n2 log n)

Cross

A

Tick

B

Cross

C

Cross

D



Question 8-Explanation: 

The recursion expression becomes: T(n) = T(n/4) + T(3n/4) + cn After solving the above recursion, we get \\theta (n*log(n)).

Question 9

Consider the Quicksort algorithm. Suppose there is a procedure for finding a pivot element that splits the list into two sub-lists each of which contains at least one-fifth of the elements. Let T(n) be the number of comparisons required to sort n elements. Then

Cross

T(n) <= 2T(n/5) + n

Tick

T(n) <= T(n/5) + T(4n/5) + n

Cross

T(n) <= 2T(4n/5) + n

Cross

T(n) <= 2T(n/2) + n



Question 9-Explanation: 

For the case where n/5 elements are in one subset, T(n/5) comparisons are needed for the first subset with n/5 elements, T(4n/5) is for the rest of 4n/5 elements, and n is for finding the pivot. If there are more than n/5 elements in one set then other sets will have less than 4n/5 elements and time complexity will be less than T(n/5) + T(4n/5) + n because the recursion tree will be more balanced.

Question 10
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 10-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.
There are 28 questions to complete.

Coding practice for sorting

 


Share your thoughts in the comments

Similar Reads