Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Posting-Version: version B 2.10 5/3/83; site umcp-cs.UUCP
Path: utzoo!watmath!clyde!burl!ulysses!mhuxl!houxm!houxz!vax135!floyd!cmcl2!seismo!rlgvax!cvl!umcp-cs!chris
From: chris@umcp-cs.UUCP
Newsgroups: net.lang
Subject: Re: A Simple Bubble Sort Function
Message-ID: <7471@umcp-cs.UUCP>
Date: Tue, 12-Jun-84 00:33:12 EDT
Article-I.D.: umcp-cs.7471
Posted: Tue Jun 12 00:33:12 1984
Date-Received: Wed, 13-Jun-84 00:02:01 EDT
References: <965@ihuxq.UUCP> <165@callan.UUCP> <131@godot.UUCP> <2096@mit-eddie.UUCP>
Organization: Univ. of Maryland, Computer Science Dept.
Lines: 40

(Editorial note: I've moved this to net.lang (from net.sources,
net.followup, and net.flame) because none of the three original
groups is a good place for it.)

Quicksort is not always the fastest sort.  In fact, in general,
it is best on a completely unsorted list (sometimes being O(n))
and worst on a completely sorted list (where it is O(n^2)).

If you are likely to have a completely (or nearly completely)
sorted list, use a Bubble Sort (or perhaps an insertion sort).
If you are likely to have a completely scrambled list, use a
quick sort.  If you have a list in which the items are usually
sorted but occasionally way out of place, you might try a Shell
sort.

For a real example, the TeX Versatec driver uses a Shell sort,
after reading in the list of all the characters on a page.  These
are either mostly sorted, with occasional things a line or two of
characters out of place, or mostly exactly unsorted by a common
distance [this is where the Shell sort wins!] because the pages
are to be printed sideways.  (To clarify a bit, this paragraph
so far would be require the following character order:
    s,a,d,c,a,a,F
    o,r,i,h,r,f,o
     ,e,s,a,e,t,r
    ...
In other words, it reads upward along the columns.)

I tried replacing the Shell sort with various other sorts, but
none of the ones I tried gave quite as good results.  (Part of
it was, no doubt, that the Shell sort code makes particularly
good use of the Vax's addressing modes.)

For another real example, a quicksort seems to work best in
Emacs's startup code in macros.c.  (It used to use a variation
on an insertion sort.)
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci (301) 454-7690
UUCP:	{seismo,allegra,brl-bmd}!umcp-cs!chris
CSNet:	chris@umcp-cs		ARPA:	chris@maryland