Re: Quicksort pathological behavior?

RobertMaas_at_YahooGroups.Com
Date: 04/01/04


Date: Wed, 31 Mar 2004 21:41:34 -0800


> From: CBFalconer <cbfalconer@yahoo.com>
> If you want guaranteed worst case performance, try heapsort (for
> arrays) or mergesort (for lists).

Three points: First, heapsort works fine for binary trees too, in fact
it's easier to understand that way. Second, mergesort works fine with
arrays too. You merge data back and forth between segments of two
different arrays. Third, heapsort isn't stable, so if your data is
originally in an array, instead of kluging a sequence number as
secondary key to make heapsort be stable, or copying the whole array to
a linked-list to mergesort then copying back to the array at the end,
you might as well directly use the two-array version of mergesort.
(Of course if your application really needs to maintain a priority
queue, where highest-priority task is popped from the queue and new
tasks are inserted asynchronously, then a heap with secondary key for
time-enqueued is the best way to achieve stable-sort behaviour.)

It seems to me, from looking at all the sorting algorithms I've seen
(run on a Von Neumann machine, i.e. regular non-quantum
single-processor), if the data is originally in an array and you want
the sorted result in the same array, you can't simultaneously achieve
all three desiderata:
(1) Worst-case behaviour guaranteed bounded by n*log(n);
(2) Stable-sort;
(3) No need to allocate additional storage.
Has anybody proven that mathematically, or found a counterexample?



Relevant Pages

  • Re: Mergesort algorithm for linked lists
    ... In fact he conditionalizes whether it is natural or not. ... To do natural mergesort with arrays requires an auxilary array where item ...
    (comp.lang.c)
  • Re: merge sort
    ... I have to use a function mergesort that takes 3 arguments - an array, ... int[] temp]. ... I dont have a problem with the merge function only with the mergesort. ... "Merging means the combination of two or more ordered files ...
    (comp.lang.c)
  • Re: merge sort
    ... array, its size, and an help array(i.e mergesort(int array, int ... function only with the mergesort. ... the demonstration uses for hashlib. ...
    (comp.lang.c)
  • Re: merge sort
    ... I have to use a function mergesort that takes 3 arguments - an array, ... int[] temp]. ... I dont have a problem with the merge function only with the mergesort. ... "Merging means the combination of two or more ordered files ...
    (comp.lang.c)
  • Re: Help needed for a sorting code in the literature
    ... >The code of the heapsort algorithm as given in books by ... Now, when your array origin is 1, "twice as far ... Greg Rose ...
    (sci.crypt)