QSORT

Portability Subroutine: Performs a quick sort on an array of rank one.

Module

USE IFPORT

Syntax

CALL QSORT (array,len,isize,compar)

array

(Input) Any type. One-dimensional array to be sorted.

If the data type does not conform to one of the predefined interfaces for QSORT, you may have to create a new interface (see below).

len

(Input) INTEGER(4) on IA-32 architecture; INTEGER(8) on Intel® 64 architecture and IA-64 architecture. Number of elements in array.

isize

(Input) INTEGER(4) on IA-32 architecture; INTEGER(8) on Intel® 64 architecture and IA-64 architecture. Size, in bytes, of a single element of array:

  • 4 if array is of type REAL(4)

  • 8 if array is of type REAL(8) or complex

  • 16 if array is of type COMPLEX(8)

compar

(Input) INTEGER(2). Name of a user-defined ordering function that determines sort order. The type declaration of compar takes the form:

INTEGER(2) FUNCTION compar(arg1, arg2)

where arg1 and arg2 have the same type as array (above). Once you have created an ordering scheme, implement your sorting function so that it returns the following:

  • Negative if arg1 should precede arg2

  • Zero if arg1 is equivalent to arg2

  • Positive if arg1 should follow arg2

Dummy argument compar must be declared as external.

In place of an INTEGER kind, you can specify the constant SIZEOF_SIZE_T, defined in IFPORT.F90, for argument len or isize. Use of this constant ensures correct compilation.

Note iconNote

If you use QSORT with different data types, your program must have a USE IFPORT statement so that all the calls work correctly. In addition, if you wish to use QSORT with a derived type or a type that is not in the predefined interfaces, you must include an overload for the generic subroutine QSORT. Examples of how to do this are in the portability module's source file, IFPORT.F90.

Compatibility

CONSOLE STANDARD GRAPHICS QUICKWIN GRAPHICS WINDOWS DLL LIB

Example

PROGRAM SORTQ

USE IFPORT

integer(2), external :: cmp_function

integer(2) insort(26), i

integer (SIZEOF_SIZE_T) array_len, array_size

array_len = 26

array_size = 2

do i=90,65,-1

insort(i-64)=91 - i

end do

print *, "Before: "

print *,insort

CALL qsort(insort,array_len,array_size,cmp_function)

print *, 'After: '

print *, insort

END

!

integer(2) function cmp_function(a1, a2)

integer(2) a1, a2

cmp_function=a1-a2

end function