home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!destroyer!cs.ubc.ca!mprgate.mpr.ca!walduck
- From: walduck@mpr.ca (Andrew Walduck)
- Subject: Re: Making qsort type-safe
- Message-ID: <1992Nov6.202732.27007@mprgate.mpr.ca>
- Sender: news@mprgate.mpr.ca
- Organization: MPR Teltech Ltd., Burnaby, B.C., Canada
- References: <1992Nov4.162131.10289@cs.brown.edu> <1992Nov5.125947.1@vax1.bham.ac.uk> <1992Nov5.160048.15113@bcrka451.bnr.ca>
- Date: Fri, 6 Nov 92 20:27:32 GMT
- Lines: 26
-
- In article <1992Nov5.160048.15113@bcrka451.bnr.ca> sjm@bcrki65.bnr.ca (Stuart MacMartin) writes:
- >The library version of qsort on some machines behaves abysmally if many
- >objects compare equal. On more than one UNIX workstation I have seen qsort
- >take more than 3/4 of an *hour* to sort a large list into two buckets,
- >whereas an appropriately adjusted qsort takes 1/4 *second*. (Yes I know
- >that qsort seems like a silly way to do what could be a single pass
- >operation, but why should "sort" be applicable to only some kinds of sorts?)
-
- It is precisely due to this behaviour that I prefer heapsort to quicksort...
- Quicksort performance can rapidly approach O(N^2) as the list becomes closer
- to being sorted!! (It depends how the pivot is chosen...)
-
- Many people think of qsort as an O(nlogn) sort...it isn't. Its average
- case is O(nlogn)...but its worst case is O(n^2) (the same as a bubble
- sort!!) This behaviour, when combined with its stack requirements, is
- why I prefer heap sort. In all cases, it performs as an O(nlogn) sort...
- and it can be implemented in 2 success loops...(about 30 lines of code
- or so...)
-
- Andrew Walduck
-
- --
- Andrew Walduck, MPR Canada, walduck@mprgate.mpr.ca
- -------------------------------------------------------------------------------
- (I hope someone will flatter me by using this as a .sig quote...)
- -- Reid Kneeland
-