comp.lang.ada
 help / color / mirror / Atom feed
From: "Wendy E. McCaughrin" <wemccaug@bluestem.prairienet.org>
Subject: Re: Quick Sort Median of Three worst case
Date: Fri, 31 Jan 2003 09:17:12 +0000 (UTC)
Date: 2003-01-31T09:17:12+00:00	[thread overview]
Message-ID: <b1deuo$5k6$1@wildfire.prairienet.org> (raw)
In-Reply-To: 588d8bf4.0301301425.586b0ae4@posting.google.com

In comp.lang.c 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) ?

 It depends upon what is meant by "running time". QuickSort basically has to
 perform three operations at each iteration/recursion: selection of a pivot,
 comparison of elements to the pivot from its left and right, and transposi-
 tions when out of order. 
 In (a), the first iteration would (using Median of 3) choose 2, 1, N/2 and
 choose N/2 as the pivot, then compare 2 and 1 to it and transpose them, and 
 then compare about N/2 - 1 remaining elements left of the pivot to it, only
 to find them in sort.  Thus no more transpositions in that first iteration
 would occur, and no further transpositions would occur in future iterations.
 But pivots would still get chosen and comparisons of elements made against 
 it, so it appears that O(n*lg(n)) comparisons would occur. (The common case
 of QuickSort said to perform O(n^2) on nearly-sorted lists is an artifact of
 always choosing some minimum or maximum element from the left or right on
 each partition.)

 In (b), the first transposition is avoided but -- since every element com-
 pared to the pivot would be out of sort, the number of transpositions as
 well as comparisions appears to be O(n*lg(n)).

 In both cases -- since Median of 3 is used to select the pivot from sorted
 lists -- the pivot manages to bisect the least into two equal halves, thus
 helping to ensure O(lg(n)) iterations from original list to smallest parti-
 tion.




  parent reply	other threads:[~2003-01-31  9:17 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 [this message]
2003-01-31  9:59 ` Albert van der Horst
replies disabled

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