SUEWS API Site
Documentation of SUEWS source code
suews_util_qsort.f95
Go to the documentation of this file.
1! Recursive Fortran 95 quicksort routine
2! sorts real numbers into ascending numerical order
3! Author: Juli Rew, SCD Consulting (juliana@ucar.edu), 9/03
4! Based on algorithm from Cormen et al., Introduction to Algorithms,
5! 1997 printing
6
7! Made F conformant by Walt Brainerd
8
10
11 IMPLICIT NONE
12 PUBLIC :: qsortc
13 PRIVATE :: partition
14
15CONTAINS
16
17 RECURSIVE SUBROUTINE qsortc(A)
18 REAL, INTENT(in out), DIMENSION(:) :: a
19 INTEGER :: iq
20
21 IF (SIZE(a) > 1) THEN
22 CALL partition(a, iq)
23 CALL qsortc(a(:iq - 1))
24 CALL qsortc(a(iq:))
25 END IF
26 END SUBROUTINE qsortc
27
28 SUBROUTINE partition(A, marker)
29 REAL, INTENT(in out), DIMENSION(:) :: a
30 INTEGER, INTENT(out) :: marker
31 INTEGER :: i, j
32 REAL :: temp
33 REAL :: x ! pivot point
34 x = a(1)
35 i = 0
36 j = SIZE(a) + 1
37
38 DO
39 j = j - 1
40 DO
41 IF (a(j) <= x) EXIT
42 j = j - 1
43 END DO
44 i = i + 1
45 DO
46 IF (a(i) >= x) EXIT
47 i = i + 1
48 END DO
49 IF (i < j) THEN
50 ! exchange A(i) and A(j)
51 temp = a(i)
52 a(i) = a(j)
53 a(j) = temp
54 ELSEIF (i == j) THEN
55 marker = i + 1
56 RETURN
57 ELSE
58 marker = i
59 RETURN
60 END IF
61 END DO
62
63 END SUBROUTINE partition
64
65END MODULE qsort_c_module
subroutine, private partition(a, marker)
recursive subroutine, public qsortc(a)