edited by
19,688 views
46 votes
46 votes

Let $P$ be quicksort program to sort numbers in ascending order using the first element as the pivot. Let $t_1$ and $t_2$ 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?

  1. $t_1 = 5$
  2. $t_1 < t_2$
  3. $t_1>t_2$
  4. $t_1 = t_2$
edited by

6 Answers

Best answer
59 votes
59 votes
it would be $t_1>t_2$, because the first case is the worst case of quicksort i.e. minimum number is chosen as pivot. Hence in the worst case the comparisons are high.

The splitting occurs as
$[1] [2345]$
$[2] [345]$
$[3] [45]$
$[4] [5]$

and

$[123] [45]$
$[1] [23] [4][5]$
$[2] [3]$

Number of recursive calls remain the same, but in second case the number of elements passed for the recursive call is less and hence the number of comparisons also less.

Correct Answer: $C$
edited by
49 votes
49 votes

Question is asking about number of comparisons. 

First case [1 2 3 4 5]

1 [2 3 4 5]    ->  4 comparisons
2 [3 4 5]  -> 3 comparisons
3 [4 5] -> 2 comparisons
[5] -> 1 comparison

Second case [4 1 5 3 2]
[1 3 2] [5] -> 4 comparisons
[3 2]  -> 2 comparisons
3 [2]  -> 1 comparison

Hence, in second case number of comparisons is less. => t1 > t2.

22 votes
22 votes
We dont even need to compare the number of comparisions we know that Quick sort gives worst result

list is already sorted ascending or decending and when all element are equal it has time complexity of O($n^{2}$)  

in rest cases it is O($nlogn$)  hence t1>t2
4 votes
4 votes

When first element or last element is chosen as pivot, Quick Sort's worst case occurs for the sorted arrays. In every step of quick sort, numbers are divided as per the following recurrence. T(n) = T(n-1) + O(n)

So, t1>t2

Answer:

Related questions

31.4k
views
10 answers
61 votes
go_editor asked Sep 28, 2014
31,351 views
You have an array of $n$ elements. Suppose you implement quicksort by always choosing the central element of the array as the pivot. Then the tightest upper bound for the worst case performance is$O(n^2)$O(n \log n)$\Theta(n\log n)$O(n^3)$
14.1k
views
4 answers
43 votes
Kathleen asked Sep 14, 2014
14,090 views
Randomized quicksort is an extension of quicksort where the pivot is chosen randomly. What is the worst case complexity of sorting n numbers using Randomized quicksort?$O(n)$O(n \log n)$O(n^2)$O(n!)$
11.9k
views
3 answers
34 votes
makhdoom ghaya asked Feb 11, 2015
11,880 views
Which one of the following is the recurrence equation for the worst case time complexity of the quick sort algorithm for sorting $n\;( \geq 2)$ numbers? In the recurrence equations given in the ... = 2T ( n - 1) + cn$T(n) = T (n/2) + cn$
55.2k
views
16 answers
89 votes
go_editor asked Sep 28, 2014
55,246 views
The minimum number of comparisons required to find the minimum and the maximum of $100$ numbers is ________