-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod_qsort.f90
67 lines (55 loc) · 1.93 KB
/
mod_qsort.f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
! Recursive Fortran 95 quicksort routine
! sorts real numbers into ascending numerical order
! Author: Juli Rew, SCD Consulting ([email protected]), 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
RECURSIVE SUBROUTINE QsortC(A)
REAL(KIND=8), INTENT(INOUT), DIMENSION(:) :: A
INTEGER :: iq
IF (SIZE(A) > 1) THEN
CALL Partition(A, iq)
CALL QsortC(A(:iq-1))
CALL QsortC(A(iq:))
END IF
END SUBROUTINE QsortC
SUBROUTINE Partition(A, marker)
REAL(KIND=8), INTENT(INOUT), DIMENSION(:) :: A
INTEGER, INTENT(OUT) :: marker
INTEGER :: i, j
REAL(KIND=8) :: temp
REAL(KIND=8) :: x ! pivot point
x = A(1)
i = 0
j = SIZE(A) + 1
DO
j = j-1
DO
IF (A(j) <= x) EXIT
j = j-1
END DO
i = i+1
DO
IF (A(i) >= x) EXIT
i = i + 1
END DO
IF (i < j) THEN
! exchange A(i) and A(j)
temp = A(i)
A(i) = A(j)
A(j) = temp
ELSEIF (i == j) THEN
marker = i + 1
RETURN
ELSE
marker = i
RETURN
ENDIF
END DO
END SUBROUTINE Partition
END MODULE qsort_c_module