From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fd7c9,f142583de41ba830 X-Google-Attributes: gidfd7c9,public X-Google-Thread: 1014db,f142583de41ba830 X-Google-Attributes: gid1014db,public X-Google-Thread: 103376,f142583de41ba830 X-Google-Attributes: gid103376,public X-Google-Thread: 108717,f142583de41ba830 X-Google-Attributes: gid108717,public X-Google-ArrivalTime: 2003-01-31 01:17:12 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news.uchicago.edu!vixen.cso.uiuc.edu!news.prairienet.org!not-for-mail From: "Wendy E. McCaughrin" Newsgroups: comp.lang.c,comp.lang.ada,comp.programming,comp.lang.forth Subject: Re: Quick Sort Median of Three worst case Date: Fri, 31 Jan 2003 09:17:12 +0000 (UTC) Organization: CNI/Prairienet Message-ID: References: <588d8bf4.0301301425.586b0ae4@posting.google.com> NNTP-Posting-Host: bluestem.prairienet.org X-Trace: wildfire.prairienet.org 1044004632 5766 192.17.3.4 (31 Jan 2003 09:17:12 GMT) X-Complaints-To: newsmgr@prairienet.org NNTP-Posting-Date: Fri, 31 Jan 2003 09:17:12 +0000 (UTC) User-Agent: tin/1.4.4-20000803 ("Vet for the Insane") (UNIX) (SunOS/5.8 (sun4u)) Xref: archiver1.google.com comp.lang.c:170430 comp.lang.ada:33634 comp.programming:50079 comp.lang.forth:30184 Date: 2003-01-31T09:17:12+00:00 List-Id: In comp.lang.c goms 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.