comp.lang.ada
 help / color / mirror / Atom feed
From: albert@spenarnc.xs4all.nl (Albert van der Horst)
Subject: Re: Quick Sort Median of Three worst case
Date: Fri, 31 Jan 2003 09:59:24 GMT
Date: 2003-01-31T09:59:24+00:00	[thread overview]
Message-ID: <H9Kp30.BK7.1.spenarn@spenarnc.xs4all.nl> (raw)
In-Reply-To: 588d8bf4.0301301425.586b0ae4@posting.google.com

In article <588d8bf4.0301301425.586b0ae4@posting.google.com>,
goms <gomskams@yahoo.co.uk> wrote:
>Hi,
>
>I would like to know the running time of the Median of Three routine
>version Quick Sort for the following input
>
>(a)      2, 3, 4, ..,N-1, N, 1
>
>What is the running time for the following input
>
>(b)      1, N, N-1, ....., 4, 3, 2
>
>Is the above said inputs (a) and (b) the worst case? and is the
>running time is O(N^2) ?
>
>I know that using Median of Three routine version of Quick Sort for,
>
>a) sorted input
>  the pivot is always in the middle, so the time would be O(nlogn)
>
>b) reverse-ordered input
>  the pivot is always in the middle, so the time would be O(nlogn)
>
>
>A reply with explanation will be great.  Thanks in Advance.

Your questions cannot be answered because they are critically
dependant on implementation details. However I can offer the
following explanation:

The worst case for a simplistic qsort is O(N^2) (where the pivot
happens to be the minimum of the remainder to be sorted.)
(This has actually occurred on sorted sets, where the first element
is chosen as a pivot.)
On a very contrived data set the Median of Three would result
in a pivot that is the second minimum item of the remainder to be
sorted.) This too would be be O(N^2).

Some qsorts use random selection for the pivot.
The data set would need to be contrived for exactly the RNG and
the random seed, but it is still possible.
Extremely unlikely, but present as a worst case.
QED

--
-- 
Albert van der Horst,Oranjestr 8,3511 RA UTRECHT,THE NETHERLANDS
Q.: Where is Bin? A.: With his ma. Q.: That makes the Saudi's
terrorists, then?  A.: No, George already owns their oil.
albert@spenarnc.xs4all.nl     http://home.hccnet.nl/a.w.m.van.der.horst



      parent reply	other threads:[~2003-01-31  9:59 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-01-30 22:25 Quick Sort Median of Three worst case goms
2003-01-30 23:51 ` Bobby D. Bryant
2003-01-31 12:28   ` Marin David Condic
2003-01-31  9:17 ` Wendy E. McCaughrin
2003-01-31  9:59 ` Albert van der Horst [this message]
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox