First time here? Checkout the FAQ!
+1 vote
What is the time complexity of quick sort when

 (i) Choosing median of sorted array as pivot.
asked in DS by Veteran (58.6k points)   | 91 views


median element divides partitions into almost equall but not 1 and (n-1) [partitions]

but the array is sorted

if array is 1,2,3,4,5,6.(median =3) assume it divides into [1,2] 3  [4,5,6]

1 Answer

+4 votes
Best answer

When we choose median as pivot , this means after applying partition the division into 2 subarrays is predefined that it will get divided into 2 halves..So recurrence relation for time will be :

          T(n)   =   2T(n/2) + O(n)  [ O(n) time is required for partition algorithm ]

==>    T(n)   =   θ(nlogn)  [ i.e. as division into subarrays is prespecified so worst case = best case = average case ]

Hence θ(nlogn) is the correct answer for the given scenario..

If however , we say central element is chosen as pivot..So it may go either at first or last or middle of array..So times will differ in that case and hence worst case will be O(n2)..

answered by Veteran (77.8k points)  
selected by
is there any difference between middle element and median elelment
As I said whenever we say median it means middle element of sorted array..But what is middle element for an unsorted array may not be the middle element of the sorted array..It may go elsewhere after applying partition algorithm..

Hope this lets u understand the difference..
ok tnks :)

Top Users Sep 2017
  1. Habibkhan

    7808 Points

  2. Warrior

    2746 Points

  3. rishu_darkshadow

    2692 Points

  4. Arjun

    2672 Points

  5. A_i_$_h

    2426 Points

  6. nikunj

    1980 Points

  7. manu00x

    1920 Points

  8. Bikram

    1854 Points

  9. makhdoom ghaya

    1770 Points

  10. SiddharthMahapatra

    1718 Points

26,239 questions
33,805 answers
31,159 users