retagged by
17,467 views
42 42 votes

Quick-sort is run on two inputs shown below to sort in ascending order taking first element as pivot

  1. $1, 2, 3, \dots n$
  2. $n, n-1, n-2, \dots, 2, 1$

Let $C_1$ and $C_2$ be the number of comparisons made for the inputs (i) and (ii) respectively. Then, 

  1. $C_1 < C_2$
  2. $C_1 > C_2$
  3. $C_1 = C_2$
  4. we cannot say anything for arbitrary $n$

5 Answers

Best answer
46 46 votes

Correct Option: C
both are the worst cases of quick sort. (assuming pivot is either first or last element)

  1. is sorted in ascending order.
  2. is sorted in descending order.
edited by
8 8 votes
1)ascending case==>Comparisons(C1)=(n-1)+(n-2)+(n-3)+.........................+2+1

                                                               =n(n-1)/2

                                                               =o(n^2)

                                             Swaps(S1)=1+1+1+............................................+1(upto n pass)

                                                               =n

                                                               =o(n)

2)Descending case==>Comparisons(C2)=(n-1)+(n-2)+(n-3)+....................................+1

                                                                 =n(n-1)/2

                                                                 =o(n^2)

                                               Swaps(S2)=(n)+(1)+(n-2)+(1)+(n-4)+...........(ascending and descending cases will come alternatively)                                           

                                                                 =3n^2/4

                                                                =o(n^2)

so C1=C2 and S1<S2

 

                                                                =
1 1 vote
I think answer is C1>C2 because in ascending input left do 1 comparison and than stops and than right do all comparison until left=right. But in descending input left keeps going until left=right and right won't do any comparison because according to algorithm right start moving only after left finds a key which is larger than pivot which is not in the case of descending input.
1 1 vote

From my POV - As both are the Worst Cases for QuickSort and Question ask for the No. of Comparisons and Both have the equal No. of Elements So, the Answer is (C) C1=C2

Please Correct me if I'm Wrong.....

edited by
Answer:
Position:
Show:

Related questions

39 39 votes
2 answers 2 answers
9.5k
9.5k views
Kathleen asked Oct 9, 2014
9,506 views
A two dimensional array $A[1..n][1..n]$ of integers is partially sorted if $\forall i, j\in [1..n-1], A[i][j] < A[i][j+1] \text{ and } A[i][j] < A[i+1][j]$The smallest it...
33 33 votes
4 answers 4 answers
15.1k
15.1k views
Kathleen asked Oct 9, 2014
15,087 views
Four jobs to be executed on a single processor system arrive at time $0$ in the order $A, B, C, D$. Their burst CPU time requirements are $4, 1, 8, 1$ time units respecti...
31 31 votes
5 answers 5 answers
13.2k
13.2k views
Kathleen asked Oct 9, 2014
13,186 views
The recurrence relation$T(1) = 2$$T(n) = 3T (\frac{n}{4}) +n$has the solution $T(n)$ equal to$O(n)$$O (\log n)$$O\left(n^\frac{3}{4}\right)$ None of the above
30 30 votes
5 answers 5 answers
11.9k
11.9k views
Kathleen asked Oct 9, 2014
11,932 views
Insert the characters of the string $K \ R \ P \ C \ S \ N \ Y \ T \ J \ M$ into a hash table of size $10$.Use the hash function$$h(x)=( ord (x) – ord (\text{“}a\text{”}...