in Algorithms edited by
15,861 views
39 votes
39 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$
in Algorithms edited by
15.9k views

4 Comments

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).
0
0
so both the methods are correct right?
0
0
yup
0
0

6 Answers

56 votes
56 votes
Best answer
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

4 Comments

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

3
3

@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 :)

Reference: https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-046j-introduction-to-algorithms-sma-5503-fall-2005/video-lectures/lecture-4-quicksort-randomized-algorithms/lec4.pdf

3
3

@SomeEarth

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

0
0
44 votes
44 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.

4 Comments

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

i m getting the same!

0
0
t1 --- > 10 comparisons and 4 swaps

t2 --- > 6 comparisons and 6 swaps

 

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

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