Sorting · Algorithms
24 questions · asked in 15 of 27 years (2000–2026)
MCQ 22 · NAT 21-mark 17 · 2-mark 7
2000Questions per year2026
Questions 6–10 of 24
Which one of the following is the recurrence equation for the worst case time complexity of the Quicksort algorithm for sorting n ( 2) numbers? In the recurrence equations given in the options below, c is a constant.
Answer: (B)
In the worst case, the pivot is the smallest or the largest element every time. So the two parts have sizes n1 and 1. Partitioning also takes linear work.
The recurrence is:
T(n) = T(n 1) + T(1) + cn
Answer: T(n) = T(n-1) + T(1) + cn
The recurrence is:
T(n) = T(n 1) + T(1) + cn
Answer: T(n) = T(n-1) + T(1) + cn
Suppose you are provided with the following function declaration in the C programming language.
int partition(int a[ ], int n);
The function treats the first element of a[] as a pivot, and rearranges the array so that all elements less than or equal to the pivot is in the left part of the array, and all elements greater than the pivot is in the right part. In addition, it moves the pivot so that the pivot is the last element of the left part. The return value is the number of elements in the left part.
The following partially given function in the C programming language is used to find the smallest element in an array a[ ] of size n using the partition function. We assume
The missing argument lists are respectively
int partition(int a[ ], int n);
The function treats the first element of a[] as a pivot, and rearranges the array so that all elements less than or equal to the pivot is in the left part of the array, and all elements greater than the pivot is in the right part. In addition, it moves the pivot so that the pivot is the last element of the left part. The return value is the number of elements in the left part.
The following partially given function in the C programming language is used to find the smallest element in an array a[ ] of size n using the partition function. We assume
int kth_smallest(int a[], int n, int k)
{
int left_end = partition(a,n);
if ( left_end+1 == k ){
return a[left_end];
}
if ( left_end+1 > k ){
return kth_smallest( ________ );
} else {
return kth_smallest( ________ );
}
} The missing argument lists are respectively
Answer: (A)
Read the code as it is written: it returns a[left_end] when left_end + 1 == k, so left_end is the 0-based index of the pivot after partition. The pivot is then the (left_end + 1)-th smallest element.
- If left_end + 1 k, the k-th smallest lies left of the pivot, in a[0 .. left_end 1]. That part starts at a and has left_end elements, so the call is (a, left_end, k).
- Otherwise the answer lies right of the pivot. That part starts at a + left_end + 1 and has n left_end 1 elements. The pivot and the left_end elements before it are all smaller, so we want the (k left_end 1)-th smallest of the right part: (a+left_end+1, n-left_end-1, k-left_end-1).
Option B starts the right-part search at a, so it searches the left part again. Options C and D put the two argument lists in the wrong cases.
Answer: (A)
- If left_end + 1 k, the k-th smallest lies left of the pivot, in a[0 .. left_end 1]. That part starts at a and has left_end elements, so the call is (a, left_end, k).
- Otherwise the answer lies right of the pivot. That part starts at a + left_end + 1 and has n left_end 1 elements. The pivot and the left_end elements before it are all smaller, so we want the (k left_end 1)-th smallest of the right part: (a+left_end+1, n-left_end-1, k-left_end-1).
Option B starts the right-part search at a, so it searches the left part again. Options C and D put the two argument lists in the wrong cases.
Answer: (A)
Assume that a mergesort algorithm in the worst case takes 30 seconds for an input of size 64. Which of the following most closely approximates the maximum input size of a problem that can be solved in 6 minutes?
Answer: (B)
The time taken by mergesort is . Take n = 64: 64 6 = 384 units, and this takes 30 s.
6 minutes is 12 times 30 s, so we get 12 30 s 12 384 = 4608 units.
Now check n = 512: 512 9 = 4608
Answer: 512
6 minutes is 12 times 30 s, so we get 12 30 s 12 384 = 4608 units.
Now check n = 512: 512 9 = 4608
Answer: 512
Let P be a quick sort program to sort numbers in ascending order using the first element as the pivot. Let t1 and t2 be the number of comparisons made by P for the inputs [1 2 3 4 5] and [4 1 5 3 2] respectively. Which one of the following holds?
Answer: (C)
Check each case:
- Sorted input [1 2 3 4 5]: every split is as uneven as possible, so 4 + 3 + 2 + 1 = 10 comparisons.
- Input [4 1 5 3 2]: pivot 4 takes 4 comparisons [2 1 3] and [5]; pivot 2 takes 2 comparisons [1] and [3]. Total 6.
Answer:
- Sorted input [1 2 3 4 5]: every split is as uneven as possible, so 4 + 3 + 2 + 1 = 10 comparisons.
- Input [4 1 5 3 2]: pivot 4 takes 4 comparisons [2 1 3] and [5]; pivot 2 takes 2 comparisons [1] and [3]. Total 6.
Answer:
You have an array of n elements. Suppose you implement quick sort by always choosing the central element of the array as the pivot. Then the tightest upper bound for the worst case performance is
Answer: (A)
Picking the middle element is still a fixed rule. Someone can build the input so that the middle element is always the smallest or the largest. Then every split gives parts of size n1 and 0.
The recurrence is:
Answer:
The recurrence is:
Answer:
Keep practising