GATE CSE
First time here? Checkout the FAQ!
x
+1 vote
77 views
What is the time complexity of quick sort when

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

O(nlogn).

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 (66.2k 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 :)

Related questions

0 votes
0 answers
1
asked in DS by vaishali jhalani Boss (6k points)   | 32 views


Top Users May 2017
  1. akash.dinkar12

    3302 Points

  2. pawan kumarln

    1776 Points

  3. Bikram

    1646 Points

  4. sh!va

    1640 Points

  5. Arjun

    1396 Points

  6. Devshree Dubey

    1272 Points

  7. Debashish Deka

    1142 Points

  8. Angkit

    1044 Points

  9. LeenSharma

    1000 Points

  10. Arunav Khare

    754 Points

Monthly Topper: Rs. 500 gift card
Top Users 2017 May 22 - 28
  1. Bikram

    732 Points

  2. Arnab Bhadra

    402 Points

  3. pawan kumarln

    402 Points

  4. bharti

    304 Points

  5. LeenSharma

    238 Points


22,822 questions
29,142 answers
65,209 comments
27,666 users