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

 (i) Choosing median of sorted array as pivot.
asked in DS by Veteran (65.1k points) 35 222 627 | 96 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 (88.9k points) 15 58 294
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 :)


Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
Top Users Oct 2017
  1. Arjun

    23462 Points

  2. Bikram

    17108 Points

  3. Habibkhan

    8560 Points

  4. srestha

    6314 Points

  5. Debashish Deka

    5458 Points

  6. jothee

    5038 Points

  7. Sachin Mittal 1

    4882 Points

  8. joshi_nitish

    4416 Points

  9. sushmita

    3996 Points

  10. Rishi yadav

    3868 Points


Recent Badges

Notable Question Sanjay Sharma
Notable Question Warrior
Notable Question sid1221
Nice Answer 2018
Good Question Arjun
Nice Question mitalitak
Great Question Arjun
Popular Question Tesla!
Nice Question makhdoom ghaya
Popular Question Sankaranarayanan P.N
27,348 questions
35,204 answers
84,249 comments
33,328 users