• recategorized by
24,211 views
59 59 votes

Let $P$ be a quicksort program to sort numbers in ascending order. Let $t_{1}$ and $t_{2}$ be the time taken by the program for the inputs $\left[1 \ 2 \ 3 \ 4\right]$ and $\left[5 \ 4 \ 3 \ 2 \ 1\right]$, respectively. Which of the following holds?

  1. $t_{1} = t_{2}$
  2. $t_{1} > t_{2}$
  3. $t_{1} < t_{2}$
  4. $t_{1}=t_{2}+5 \log 5$ 

7 Answers

Best answer
78 78 votes
Actually, in both the cases, it will take $O(n^{2})$ time for partition algorithm and $T(n-1)$ time for subproblem. As $n$ is the number of inputs and in the $2^{\text{nd}}$ case inputs are $5($greater than $1^{\text{st}}$ one that is $4),t_{1}<t_{2}.$

Correct Answer: C.
• edited by
18 18 votes

In this questions ,they have asked the running time and not number of comparisons or swaps.

Time complexity with depend on n.

Since ,both the inputs [1,2,3,4] and [5,4,3,2,1] are already sorted, so both take O(n^2) time.

(a) is correct.

4 4 votes
It will be option A.t1 = t2 ,if the list is already sorted in ascending,descending or even all elements in the list are same (all elements identical) it will be have worst case partion for quicksort and complexity will be O(n^2).
2 2 votes

In the ascending order thing we don't have to swap any element and we just have to compare with the remaining elements.

Where as in descending order things we have to compare as well as SWAP  the element.

The time complexity of both the algorithm will be O(n2)but the time taken for descending order will be greater than the time taken for the ascending order.

0 0 votes
They said that time taken by input 1 and 2 are t1 and t2 not time complexity. So we consider value here.
0 0 votes
in this question they have asked about time taken not time complexity . they wanted to compare time exact time consumed between t1 and t2 inputs .

even . if we take a pivot which yeald you to O(nlogn) at best take .

the comparison would be :

k*(4*log(4)) time for t1 input < k*(5*log(5)) time for t2 input . k depends on system on which algorithm is runned and also on specific steps and comparisons . since we are running on the same quicksort algorithm . it maximum depend on system .
 

now let compare for worst case . it will happen when pivot is placed in last position everytime :

T(n) = t(n-1)+n = O(n^2).

on comparing time consumed on worst case :

k*(4^2) < k*(5^2) , k depends on system on which algorithm is runned and also on specific steps and comparisons . since we are running on the same quicksort algorithm . it maximum depend on system .

in both cases t2>t1 . therefore answer is Option C .
• edited by
Answer:
Position:
Show:

Related questions

2 2 votes
2 answers 2 answers
2.5k
2.5k views
Misbah Ghaya asked Nov 15, 2016
2,544 views
The relative costs of assigning jobs $J_{1}, J_{2}$ and $J_{3}$ to machines $M_{1}, M_{2}$ and $M_{3}$ are given below:$$\begin{array}{|c|cccc|}\hline\textbf{JOBS} && \te...
34 34 votes
7 answers 7 answers
9.8k
9.8k views
Misbah Ghaya asked Nov 14, 2016
9,802 views
Solve the recurrence equations:$T(n) = T(n - 1)+ n$$T(1) = 1$
52 52 votes
3 answers 3 answers
19.2k
19.2k views
Misbah Ghaya asked Feb 11, 2015
19,191 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 recurrenc...
62 62 votes
9 answers 9 answers
29.7k
29.7k views
go_editor asked Sep 26, 2014
29,726 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 i...