home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!sun-barr!cs.utexas.edu!torn!nott!bnrgate!bcrka451!bcrki65!sjm
- From: sjm@bcrki65.bnr.ca (Stuart MacMartin)
- Subject: Re: Making qsort type-safe
- Message-ID: <1992Nov10.134651.19803@bcrka451.bnr.ca>
- Sender: 5E00 Corkstown News Server
- Organization: Bell-Northern Research Ltd., Ottawa, Canada
- References: <1992Nov6.202732.27007@mprgate.mpr.ca> <dak.721261218@messua> <1992Nov09.155723.112061@Cookie.secapl.com>
- Date: Tue, 10 Nov 1992 13:46:51 GMT
- Lines: 37
-
- In article <1992Nov09.155723.112061@Cookie.secapl.com> frank@Cookie.secapl.com (Frank Adams) writes:
- >In article <dak.721261218@messua> dak@messua.informatik.rwth-aachen.de (David Kastrup) writes:
- >
- >>If you are sorting linked lists, however, use a mergesort
- >
- >Absolutely.
-
- I have sometimes sorted linked lists by creating an array of pointers to data,
- sorting this array using my modified qsort, and then adjusting the linked list
- pointers. I have not noticed this to be grossly inefficient. That way I just
- had a few lines of coding to do to be able to use existing code. Is there a
- problem with my approach?
-
- Also, if the comparison function is at all complicated (like comparing field a,
- and if that is equal, compare field b) I do a first pass through the data to
- construct a temporary array of the fields I want to compare in the correct order.
- The purpose of this is to simplify the comparison function as much as possible.
- It also reduces paging problems with qsort on huge collections. If I want to
- do a pass like this anyway, I see no reason to have the temporary collection
- in a linked list. So what exactly do you mean by "Absolutely"? Under all
- circumstances it is the best thing to do?
-
- On a slight variation of this topic: the Lipmann code for qsort compares
- two objects using operator<(). That is, "a < b". But there can be more than
- one sort order on a collection. I could sort people by name or by birth date.
- The actual ordering of the objects in the collection is a property of the
- collection, not of the objects themselves. How is this best resolved?
- Should the objects in the collection be able to compare themselves according
- to different orderings? Is operator<() enough? Surely it is not a good idea
- to have the collection class know details of how to sort the objects in the
- collection.
-
- 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. :
-