edited by
29,275 views
61 61 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$

9 Answers

Best answer
75 75 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
68 68 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.

30 30 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 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

2 2 votes

Hoare partition (Given : pivot = first element. Generally we can take any elememt as pivot)

Counting rules  :

  1. Count every pivot-vs-element comparison inside scans (including pivot with itself). 
  2. Do not count "Stopping test $i \ge j$ comparisons".

Notation  
Blue box = pointer $i$  
Red box = pointer $j$  
Nested box = $i=j$

Algorithm (one partition on $A[L..R]$)

pivot $= A[L]$, $i=L$, $j=R$  

repeat  
i-scan: while $A[i]<$ pivot do $i\leftarrow i+1$  
j-scan: while $A[j]>$ pivot do $j\leftarrow j-1$  
if $i<j$ then swap $A[i],A[j]$, $i\leftarrow i+1$, $j\leftarrow j-1$  
else stop

Case 2: Input $[4,1,5,3,2]$

Partition on $[4,1,5,3,2]$, pivot $=4$

Start
\[
\begin{array}{ccccc}
\color{blue}{\boxed{4}} & 1 & 5 & 3 & \color{red}{\boxed{2}}
\end{array}
\]

Step 1: i-scan  
$4<4$ (false)

Comparisons = 1
\[
\begin{array}{ccccc}
\color{blue}{\boxed{4}} & 1 & 5 & 3 & \color{red}{\boxed{2}}
\end{array}
\]

Step 2: j-scan  
$2>4$ (false)

Comparisons = 1
\[
\begin{array}{ccccc}
\color{blue}{\boxed{4}} & 1 & 5 & 3 & \color{red}{\boxed{2}}
\end{array}
\]

Swap 1: swap 4 and 2
\[
\begin{array}{ccccc}
\color{blue}{\boxed{2}} & 1 & 5 & 3 & \color{red}{\boxed{4}}
\end{array}
\]

Update pointers: $i\leftarrow i+1,\ j\leftarrow j-1$
\[
\begin{array}{ccccc}
2 & \color{blue}{\boxed{1}} & 5 & \color{red}{\boxed{3}} & 4
\end{array}
\]

Step 3: i-scan  
$1<4$ (true), $5<4$ (false)

Comparisons = 2
\[
\begin{array}{ccccc}
2 & 1 & \color{blue}{\boxed{5}} & \color{red}{\boxed{3}} & 4
\end{array}
\]

Step 4: j-scan  
$3>4$ (false)

Comparisons = 1
\[
\begin{array}{ccccc}
2 & 1 & \color{blue}{\boxed{5}} & \color{red}{\boxed{3}} & 4
\end{array}
\]

Swap 2: swap 5 and 3
\[
\begin{array}{ccccc}
2 & 1 & \color{blue}{\boxed{3}} & \color{red}{\boxed{5}} & 4
\end{array}
\]

Update pointers: $i\leftarrow i+1,\ j\leftarrow j-1$
\[
\begin{array}{ccccc}
2 & 1 & \color{red}{\boxed{3}} & \color{blue}{\boxed{5}} & 4
\end{array}
\]

Partition-1 totals  
Comparisons $=1+1+2+1=5$  
Swaps $=2$

Subarrays: $[2,1,3]$ and $[5,4]$

Partition on $[2,1,3]$, pivot $=2$

Start
\[
\begin{array}{ccc}
\color{blue}{\boxed{2}} & 1 & \color{red}{\boxed{3}}
\end{array}
\]

Step 1: i-scan  
$2<2$ (false)

Comparisons = 1
\[
\begin{array}{ccc}
\color{blue}{\boxed{2}} & 1 & \color{red}{\boxed{3}}
\end{array}
\]

Step 2: j-scan  
$3>2$ (true), $1>2$ (false)

Comparisons = 2
\[
\begin{array}{ccc}
\color{blue}{\boxed{2}} & \color{red}{\boxed{1}} & 3
\end{array}
\]

Swap: swap 2 and 1
\[
\begin{array}{ccc}
\color{blue}{\boxed{1}} & \color{red}{\boxed{2}} & 3
\end{array}
\]

Update pointers
\[
\begin{array}{ccc}
\color{red}{\boxed{1}} & \color{blue}{\boxed{2}} & 3
\end{array}
\]

 

Partition-2 totals  
Comparisons $=1+2=3$  
Swaps $=1$

Remaining subarray $[2,3]$

Partition on $[2,3]$, pivot $=2$

Start
\[
\begin{array}{cc}
\color{blue}{\boxed{2}} & \color{red}{\boxed{3}}
\end{array}
\]

Step 1: i-scan  
$2<2$ (false)

Comparisons = 1
\[
\begin{array}{cc}
\color{blue}{\boxed{2}} & \color{red}{\boxed{3}}
\end{array}
\]

Step 2: j-scan  
$3>2$ (true), $2>2$ (false)

Comparisons = 2

$i=j$ (double box)
\[
\begin{array}{cc}
\color{blue}{\boxed{\color{red}{\boxed{2}}}} & 3
\end{array}
\]

Partition-3 totals  
Comparisons $=1+2=3$  
Swaps $=0$

Partition on $[5,4]$, pivot $=5$

Start
\[
\begin{array}{cc}
\color{blue}{\boxed{5}} & \color{red}{\boxed{4}}
\end{array}
\]

Step 1: i-scan  
$5<5$ (false)

Comparisons = 1

Step 2: j-scan  
$4>5$ (false)

Comparisons = 1

Swap: swap 5 and 4
\[
\begin{array}{cc}
\color{blue}{\boxed{4}} & \color{red}{\boxed{5}}
\end{array}
\]

Partition-4 totals  
Comparisons $=2$  
Swaps $=1$

Case 2 total comparisons
\[
t_2 = 5+3+3+2 = 13
\]

0 0 votes

We don’t need to do any calculations here. A sorted list is known to produce the worst case time complexity for quicksort, which is O(n²). Since the first list is already in ascending order (sorted), its running time becomes O(n²).

The second list is unsorted, so quicksort runs in its average case of O(n log n).

Therefore, t1 > t2, and option C is correct.

Answer:
Position:
Show:

Related questions

90 90 votes
10 answers 10 answers
43.9k
43.9k views
go_editor asked Sep 28, 2014
43,880 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...
78 78 votes
11 answers 11 answers
33.3k
33.3k views
go_editor asked Sep 28, 2014
33,324 views
Consider a $6$-stage instruction pipeline, where all stages are perfectly balanced. Assume that there is no cycle-time overhead of pipelining. When an application is exec...
8 8 votes
6 6 answers
3.8k
3.8k views
Arjun asked Feb 27, 2025
3,839 views
Suppose that insertion sort is applied to the array $[1,3,5,7,9,11, x, 15,13]$ and it takes exactly two swaps to sort the array. Select all possible values of $x$.$10$$12...
59 59 votes
4 answers 4 answers
20.7k
20.7k views
Kathleen asked Sep 14, 2014
20,732 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...