Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!bloom-beacon!gatech!prism!vsserv!loligo!pepke
From: pepke@loligo (Eric Pepke)
Newsgroups: comp.sys.mac.hypercard
Subject: Re: Sorting gets very slow
Message-ID: <218@vsserv.scri.fsu.edu>
Date: 26 Sep 89 16:19:44 GMT
References: <818taylorj@yvax.byu.edu>
Sender: news@vsserv.scri.fsu.edu
Reply-To: pepke@loligo.UUCP (Eric Pepke)
Organization: Supercomputer Computations Research Institute
Lines: 33

In article <818taylorj@yvax.byu.edu> taylorj@yvax.byu.edu writes:
>Dan Allen said HyperCard uses a QuickSort routine (and if Dan Allen says so, I
>believe it!).  Unlike some sorts, QuickSort's speed does not improve if the
>data is already sorted (or mostly sorted).  It shouldn't actually be any slower
>than normal, but it won't be any faster either.

If I remember correctly, the speed of QuickSort is based on the ability
accurately to guess the median of a range in constant time.  If this is
done perfectly all the time, the algorithm is O(n log n).  If this is done
poorly, the algorithm is O(n^2).  One way of guessing the median which
is often presented as a bad way is to take the first three values and
average them.  In this case, the more sorted the data, the slower the
algorithm would be.

Of course, there are much better ways to guess the median.  However, it 
still remains a best case O(n log n), worst case O(n^2) algorithm.  If you
are clever, you can get the expected case to be O(n log n).

I don't understand why QuickSort is so popular, because there are plenty
of good, easy, stable sorts that give worst case O(n log n) performance and 
best case O(n).  Maybe people are misled by the name, or maybe everybody 
starts from the source to qsort.

Then again, I don't understand why some people buy double-strength pain
reliever that costs three times as much, either.

Eric Pepke                                     INTERNET: pepke@gw.scri.fsu.edu
Supercomputer Computations Research Institute  MFENET:   pepke@fsu
Florida State University                       SPAN:     scri::pepke
Tallahassee, FL 32306-4052                     BITNET:   pepke@fsu

Disclaimer: My employers seldom even LISTEN to my opinions.
Meta-disclaimer: Any society that needs disclaimers has too many lawyers.