home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!utcsri!torn!cunews!nrcnet0!bnrgate!bcrka451!bcrki65!sjm
- From: sjm@bcrki65.bnr.ca (Stuart MacMartin)
- Subject: Re: Making qsort type-safe
- Message-ID: <1992Nov5.160048.15113@bcrka451.bnr.ca>
- Sender: 5E00 Corkstown News Server
- Organization: Bell-Northern Research Ltd., Ottawa, Canada
- References: <1992Nov4.162131.10289@cs.brown.edu> <1992Nov5.125947.1@vax1.bham.ac.uk>
- Date: Thu, 5 Nov 1992 16:00:48 GMT
- Lines: 33
-
- 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?)
-
- The code that is in Lipmann is not good enough if you do a lot of sorting
- of large arrays. It has none of the standard modifications to handle
- equal elements efficiently, to use minimal stack, etc. It is there to
- show the essence of qsort and how this would be handled with templates,
- for instructional purposes (or perhaps for use in simple applications).
-
- If you are sorting large arrays (or more generally, collections), you are
- better off sorting pointers. This way you only swap pointers, not objects.
- [Also, you can then keep several sort orders for the same collection, if you
- want.]
-
- If you might be sorting collections that are huge and will require paging
- or that have other I/O implications, you may want to use a different sort
- algorithm. Is it wise to make the sorting algorithm visible outside of
- the class? (e.g. by using the name "qsort")
-
- So about this not re-inventing the wheel business: be careful what you reuse.
- Make sure it satisfies your requirements. The blanket statements made by
- previous posters about reusing qsort because it is there are oversimplifying
- things, IMO.
-
- Stuart
- --
- : Stuart MacMartin email: sjm@bnr.ca :
- : Bell-Northern Research phone: (613) 763-5625 :
- : PO Box 3511, Stn C, Ottawa, K1Y-4H7, CANADA Standard disclaimers apply. :
-