adapting a quicksort
  Home FAQ Contact Sign in
comp.lang.fortran only
 
Advanced search
POPULAR GROUPS

more...

comp.lang.fortran Profile…
 Up
adapting a quicksort         


Author: Ron Ford
Date: Aug 23, 2008 18:51

I recently bumped into fortran.com as a resource and continue to be
impressed with its content. I found a quicksort for reals there and wanted
to adapt it:

http://www.fortran.com/qsort_c.f95

For starters, I want it to sort integers instead. Is that change as simple
as replacing 'real' with 'integer' in the following:

! Recursive Fortran 95 quicksort routine
! sorts real numbers into ascending numerical order
! Author: Juli Rew, SCD Consulting (juliana@ucar.edu), 9/03
! Based on algorithm from Cormen et al., Introduction to Algorithms,
! 1997 printing

! Made F conformant by Walt Brainerd

module qsort_c_module

implicit none
public :: QsortC
private :: Partition

contains
Show full article (2.08Kb)
5 Comments
Re: adapting a quicksort         


Author: Ron Ford
Date: Aug 23, 2008 19:42

On Sat, 23 Aug 2008 19:51:44 -0600, Ron Ford posted:
> I recently bumped into fortran.com as a resource and continue to be
> impressed with its content. I found a quicksort for reals there and wanted
> to adapt it:
>
> http://www.fortran.com/qsort_c.f95
>
> For starters, I want it to sort integers instead. Is that change as simple
> as replacing 'real' with 'integer' in the following:

Apparently, it is:

! tja

module sort3
implicit none
private
public :: selection_sort
! type definition includes only an integer
type, public :: address
integer :: zip_code
end type address
contains
recursive...
Show full article (7.51Kb)
no comments
Re: adapting a quicksort         


Author: ttw6687
Date: Aug 23, 2008 20:47

The pivot is the split point. Items larger go into one set of lists
and smaller go into the other. Kunth describes in his volume on
sorting and searching.

Note that the above code is correct unless arithmetic bounds get
exceeded. This depends on the compiler and hardware. It's possible
that A-B.LT.0 does not imply that A.LT.B, even for integers.
no comments
Re: adapting a quicksort         


Author: glen herrmannsfeldt
Date: Sep 2, 2008 12:57

Ron Ford wrote:
(snip)
> It seems to work right out of the can for me. With a sort size of ten
> thousand, the quicksort is three orders of magnitude faster.
> Is qsort an intrinsic for C?

Yes, but despite the name it isn't guaranteed to
implement quicksort.

The advantage is that it can sort an array of any kind
of data structure given a pointer to the array (usual
for C), the length, the length of each array element
(which must be in contiguous storage), and a comparison
function. The comparison function is given pointers to
two elements and returns a positive, zero, or negative
value if the first is greater then, equal to, or less
than the second. With the appropriate compare function
the array can be an array of pointers, or structures
containing pointers.

To do that in Fortran 2003 might also require a routine
to copy such array elements.
Show full article (0.88Kb)
no comments
Re: adapting a quicksort         


Author: David Thompson
Date: Sep 6, 2008 21:41

On Mon, 25 Aug 2008 15:53:04 -0600, Ron Ford
wrote:
> It seems to work right out of the can for me. With a sort size of ten
> thousand, the quicksort is three orders of magnitude faster.
>
> Is qsort an intrinsic for C?

Well, a sort routine named qsort() is part of the standard library in
C. (They don't use the term 'intrinsic' but it's the same idea.)

But it isn't required to be quicksort; the algorithm is up to the
choice of the implementor
-- as long as it does indeed sort, given a
sane comparison routine. A good implementor will document what that
algorithm is, and a great one might even give you a choice.

- formerly david.thompson1 || achar(64) || worldnet.att.net
no comments
Re: adapting a quicksort         


Author: ttw6687
Date: Sep 7, 2008 06:25

One does need to know the type of sort being used. Primarily whether
it's stable and the speed. For general use, I like a list merge sort;
it's of order N*Log(N)and stable. For things too big for memory, there
are polyphase merge.

For my own non-stable use, I just use heapsort as it only requires a
short program.
no comments