Re: qsort and arbitrary types
From: glen herrmannsfeldt (gah_at_ugcs.caltech.edu)
Date: 06/10/04
- Next message: Hans-Bernhard Broeker: "Re: Subroutine Argument Checking"
- Previous message: Toon Moene: "Re: Subroutine Argument Checking"
- In reply to: James Giles: "Re: qsort and arbitrary types"
- Next in thread: James Giles: "Re: qsort and arbitrary types"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Thu, 10 Jun 2004 21:10:33 GMT
James Giles wrote:
(snip)
> Well first, in many versions of QSORT (and other sorting
> routines), the actual data is not swapped around at all. Instead,
> an array of integers is produces which contains the indices into
> the data array which express the proper ordering. That is, the
> result array used as a vector subscript produces an ordered
> array: ARRAY(RESULT(:)) is ordered. Hence, you really
> only need the reference to a comparison function if your
> sort does this.
The C qsort() function will sort whatever array is passed
to it, as long as the comparison function returns the right
value. You can sort an array of pointers to structures
or array elements, of pointers to pointers, or the structures
themselves. It is up to the user to decide.
There is a section in Knuth's volume 3, Sorting and Searching,
where he considers external sort (data on tape or disk).
One could read in only the keys and sort them, along with
an indicator of the position of the record in the file,
and later reorder the records in the indicated order.
It turns out to be about as much work to reorder the data
as it would have been to sort it in the first place!
> Second, if your sort actually moves the data and doesn't produce
> a permutation of the indices, you only need a conditional SWAP
> routine. That is, the comparison and the swap can be combined in
> the same referenced procedure. That'why my comparison was
> called SWAP in the sample code I posted.
There are some sort algorithms that work that way, but
many do not. Quicksort, for each pass, involves arranging data
based on comparison with one element, so the swap and compare
can't be combined.
-- glen
- Next message: Hans-Bernhard Broeker: "Re: Subroutine Argument Checking"
- Previous message: Toon Moene: "Re: Subroutine Argument Checking"
- In reply to: James Giles: "Re: qsort and arbitrary types"
- Next in thread: James Giles: "Re: qsort and arbitrary types"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|