home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!ferkel.ucsb.edu!taco!rock!stanford.edu!agate!spool.mu.edu!darwin.sura.net!wupost!uwm.edu!rpi!batcomputer!munnari.oz.au!goanna!ok
- From: ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe)
- Newsgroups: comp.lang.c
- Subject: Re: qucik, merge, shell and bubble sort
- Message-ID: <15985@goanna.cs.rmit.oz.au>
- Date: 17 Nov 92 04:55:48 GMT
- References: <1e1cnvINNt2t@usenet.INS.CWRU.Edu>
- Organization: Comp Sci, RMIT, Melbourne, Australia
- Lines: 38
-
- In article <1e1cnvINNt2t@usenet.INS.CWRU.Edu>, cc015@cleveland.Freenet.Edu (Vito T. Kwan) writes:
- > Hi all,
- > I want to take a look at these four sorting method in C, does anyone
- > know where can I get (ftp) them?
- > Thankss in advance
-
- Costs:
- Bubble sort is O(N**2). It has no known uses. It is bad even for
- an O(N**2) algorithm; if you want a very simple sorting algorithm
- you can remember and that will work well, pick insertion sort.
-
- Shell sort is O(N**1.5) -ish. It has no special advantages.
-
- Quick sort is O(N**2) in the worst case. It is complicated and
- unstable. Its _average_ cost is O(N.lgN). The only known reason
- for using quicksort is if you are DESPERATELY short of memory; it
- was invented for use on a computer with 256 words of main memory.
- (Read Hoare's paper in Computer Journal 1961.) At the time it was
- invented, it was known to be inferior to merge sort by every metric
- except space, and if you are sorting a list it doesn't save any
- space either.
-
- Merge sort is O(N.lgN) in the worst cast. If the comparisons are
- not trivial (i.e. if the compiler does not know at compile time that
- they are machine integer comparisons) then the *worst* case of
- merge sort is better than the *best* case of quick sort.
-
- Of the four methods, the only one that can be recommended for general use
- is merge sort. If you have to write a sort routine for an embedded controller
- or some other application where there is almost no main memory and no backing
- store, quick sort is useful. For everyday programming in C, you should try
- using the built-in qsort() function. Most C libraries I have come across use
- some version of quick-sort, which makes them rather slow, but at least it is
- there and debugged.
-
- Your best source of such algorithms would be a textbook like
- Sedgewick's "Algorithms in C".
-
-