16,303 views

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$

Can anyone tell how the no. of comparisons is 16 in 4 1 5 3 2??
edited

ASNR1010

i am getting  10 comparisons in  [12345]  and  8 comparisons in [41532].

Now, I got how it is 16. No. of comparisons got increased because we are not using the quicksort algorithm of CLRs book instead of we are using a different one.Check comments below the best answer...
edited by
Actually to be more specific I have to say two different partition procedure both are correct. In one way you have one fix variable and you are moving the other to get partition(your's). You can also get the same partition by using a two variables in which one pointing to last and another just subsequent of the pivot and checking by pivot until one crosses the other and that should be replaced with pivot to get the partition and here, I'm considering pivot to 1st element(in this method you get 16).
so both the methods are correct right?
yup

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$

Kushagra, your partitioning algo considers right element of the unsorted part/array as pivot. If you consider left element as pivot then working procedure might be different..so exact number of comparisons will depend on the implementation..

Quick sort using first element as pivot

@Verma Ashish

I think the algorithm shared by @Kushagra गुप्ता is from CLRS book. It uses the first element as pivot element. Even I think there are only (n-1) comparisons required at each partition.

Below is the screenshot of the slide from MIT-OCW. This is the lecture slide of Charles E. Leiserson(CLRS) himself :)

Can you tell how for 4 1 5 3 2 calculations comes out to be 16.??

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.

this seems intuitively correct to me though selected answer has different logic. I wonder which is correct. :(

i m getting the same!

t1 --- > 10 comparisons and 4 swaps

t2 --- > 6 comparisons and 6 swaps

t1 > t2  ( comparisons )
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

### 1 comment

For The given cases How Your logic works Please explain. Which will you select as pivot to get O(n^2) as the time complexity for above list

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

1
27,027 views