-
Notifications
You must be signed in to change notification settings - Fork 46
sort
Fabian Kindermann edited this page May 6, 2024
·
5 revisions
subroutine sort(x, iorder)
This subroutine sorts an array x
in ascending order. The array can either be of type integer
or real*8
. The subroutine can also return the ordering permutation.
-
integer :: x(:)
orreal*8 :: x(:)
An array of arbitrary length that should be sorted in ascending order.
-
integer :: x(:)
orreal*8 :: x(:)
After the sorting process is finished, the subroutine stores the sorted array in the variablex
.
-
integer :: iorder(:)
In this one-dimensional array that must have exactly the same length as the arrayx
, the subroutine stores the ordering permutation, meaning that for any element of the original input arrayx
, the subroutine stores where this element went after the sorting process. If, hence,iorder(1) = 3
, then the first element of the sorted array will be the third element of the original arrayx
. Recording the permutation ordering is useful, if you have other arrays that should be rearranged in the same way asx
.
- Parts of this routine were copied and adapted from:
- The Quicksort algorithm in the Wikibook “Algorithm Implementation”.
- The Qsort implementation in the A FORTRAN 90 Numerical Library (AFNL)
- For further reading refer to:
- Cormen, T.H., Leiserson, C.E., Rivest, R.L. & Stein, C. (2009). Introduction to Algorithms, 3rd edition. Cambridge: MIT Press.
- This routine is used in the following programs:
prog04_08.f90
prog04_09.f90
prog10_04.f90