Fortran Library Reference

qsort,qsort64: Sort the Elements of a One-dimensional Array

The subroutine is called by:

call qsort( array, len, isize, compar )

call qsort64( array, len8, isize8, compar )

array

array

Input 

Contains the elements to be sorted  

len

INTEGER*4

Input 

Number of elements in the array.  

len8INTEGER*8 Input Number of elements in the array

isize

INTEGER*4

Input 

Size of an element, typically:  

4 for integer or real  

8 for double precision or complex  

16 for double complex  

Length of character object for character arrays  

isize8

INTEGER*8

Input 

Size of an element, typically:  

4_8 for integer or real  

8_8 for double precision or complex  

16_8 for double complex  

Length of character object for character arrays  

compar

function name 

Input 

Name of a user-supplied INTEGER*2 function.

Determines sorting order: compar(arg1,arg2)

Use qsort64 in 64-bit environments with arrays larger than 2 Gbytes. Be sure to specify the array length, len8, and the element size, isize8, as INTEGER*8 data. Use the Fortran 90 style constants to explicitly specify INTEGER*8 constants.

The compar(arg1,arg2) arguments are elements of array, returning:

Negative 

If arg1 is considered to precede arg2

Zero 

If arg1 is equivalent to arg2

Positive 

If arg1 is considered to follow arg2

For example:


demo% cat tqsort.f
    external compar
    integer*2 compar
    INTEGER*4 array(10)/5,1,9,0,8,7,3,4,6,2/, len/10/, isize/4/
    call qsort( array, len, isize, compar )
    write(*,'(10i3)') array
    end
    integer*2 function compar( a, b )
    INTEGER*4 a, b
    if ( a .lt. b ) compar = -1
    if ( a .eq. b ) compar = 0
    if ( a .gt. b ) compar = 1
    return
    end
demo% f77 -silent tqsort.f 
demo% a.out 
  0 1 2 3 4 5 6 7 8 9