in Algorithms edited by
647 views
3 votes
3 votes

If we have an array of size $\mathrm{N}$ with only $3$ different values for its elements, what is the probability that the first partition results in a completely sorted array? Assume there are an equal number of each element in the array.

  1. $1 / 2$
  2. $1 / 3$
  3. $1 / 4$
  4. $2 / 3$
in Algorithms edited by
647 views

1 comment

Choosing the median element would guarantee that the elements <= the median will be on the left of pivot and those >= the median will be to its right. But how can it be guaranteed that the order will also be maintained? Wouldn’t that depend on the implementation of the partition function?
0
0

2 Answers

5 votes
5 votes
If the median value gets picked as the pivot, the array will be in sorted order after only a single pivot. If the smallest or largest gets picked, we're not quite done.

Probability of choosing median $= 1/3$ since there are just three types of values in equal proportion.
0 votes
0 votes
We know that quick sort will give best time complexity when partition will divide there array in two equal parts every time.ie, when every time pivot element selected will be the median of the array(sorted).

 

NOTE:- BUT FOR THAT WE HAVE TO PRIOR ONLY KNOW ABOUT THE ELEMENTS WHICH IS NOT ALWAYS POSSIBLE.

 

THEREFORE, of that will happen then:-T(n) equals 2T(n/2) + (n+1).    Which is like the merge sort therefore time complexity equals. N log N.

 

Note:-in this question this concept does not required to be applied so as such😁

 

 

But here in this question we know about the elements. So if we select the median element 1st time then after the 1st iteration only the elements will be sorted.

Therefore, probability equals (n/3) by n ,equals 1/3.
Answer:

Related questions