QuickSort
QuickSort algorithm for sorting.
At present QuickSort
generic method contains following interfaces.
CALL QuickSort(vec1, low, high)
CALL QuickSort(vec1, vect2, low, high)
CALL QuickSort(vec1, vect2, vect3, low, high)
CALL QuickSort(vect1, vect2, vect3, vect4, low, high)
Interface 1
- ܀ QuickSort(vect1, low, high)
- ️܀ See example
- ↢
MODULE RECURSIVE PURE SUBROUTINE QuickSort(vect1, low, high)
Int8|Int16|Int32|Int64|Real32|Real64, INTENT(INOUT) :: vect1(:)
INTEGER(I4B), INTENT(IN) :: low, high
END SUBROUTINE QuickSort
In this interface vect1
can be a one dimensional fortran array of
Int8
,Int16
,Int32
,Int64
Real32
,Real64
In this example we test QuickSort
method.
program main
use easifembase
implicit none
integer(i4b), allocatable :: intvec( : )
real(dfp), allocatable :: realvec( : )
intvec = [ 5, 4, 3, 2, 1 ]
call QuickSort( vect1=intvec, low=1, high=SIZE(intvec) )
call display( intvec, "intvec = " )
See results
intvec =
---------
1
2
3
4
5
realvec = [ 5, 4, 3, 2, 1 ]
call QuickSort( realvec, 1, SIZE( realvec ) )
call display( realvec, "realvec = " )
See results
realvec =
----------
1.00000
2.00000
3.00000
4.00000
5.00000
end program main
Interface 2
- ܀ QuickSort(vect1, vect2, low, high)
- ️܀ See example
- ↢
INTERFACE
MODULE RECURSIVE PURE SUBROUTINE QuickSort(vect1, vect2, low, high)
INTEGER( I4B )| REAL( DFP ) , DIMENSION(:), INTENT(INOUT) :: vect1
INTEGER( I4B )| REAL( DFP ) , DIMENSION(:), INTENT(INOUT) :: vect2
INTEGER(I4B), INTENT(IN) :: low, high
END SUBROUTINE QuickSort
END INTERFACE
In this interface vect1
and vect2
can be a one dimensional fortran array of
Int32
orI4B
4 byte integersReal(DFP)
default floating point, usuallyReal64
In this example we test QuickSort
method.
program main
use easifembase
implicit none
integer(i4b), allocatable :: intvec( : )
real(dfp), allocatable :: realvec( : )
intvec = [ 5, 4, 3, 2, 1 ]
realvec = [ 8, 9, 12, 3, 2 ]
call QuickSort( vect1=intvec, vect2=realvec, low=1, high=SIZE(intvec) )
call display( intvec, "intvec = ", advance="NO" )
call display( realvec, "realvec= ", advance="YES" )
See results
intvec = , realvec=
---------, ---------
1 , 2.0000
2 , 3.0000
3 , 12.0000
4 , 9.0000
5 , 8.0000
intvec = [ 5, 4, 3, 2, 1 ]
realvec = [ 8, 9, 12, 3, 2 ]
call QuickSort( vect1=realvec, vect2=intvec, low=1, high=SIZE(intvec) )
call display( intvec, "intvec = ", advance="NO" )
call display( realvec, "realvec= ", advance="YES" )
See results
intvec = , realvec=
---------, ---------
1 , 2.0000
2 , 3.0000
5 , 8.0000
4 , 9.0000
3 , 12.0000
end program main
Interface 3
- ܀ QuickSort(vect1, vect2, vect3, low, high)
- ️܀ See example
- ↢
INTERFACE
MODULE PURE RECURSIVE SUBROUTINE QuickSort(vect1, vect2, vect3, &
& low, high)
INTEGER(I4B) | REAL(DFP), DIMENSION(:), INTENT(INOUT) :: vect3
REAL(DFP) | INTEGER(I4B), DIMENSION(:), INTENT(INOUT) :: vect1, vect2
INTEGER(I4B), INTENT(IN) :: low, high
END SUBROUTINE QuickSort
END INTERFACE
In this interface vect1
, vect2
, and vect3
can be a one dimensional fortran array of
Int32
orI4B
4 byte integersReal(DFP)
default floating point, usuallyReal64
In this example we test QuickSort
method.
program main
use easifembase
implicit none
integer(i4b), allocatable :: intvec( : )
real(dfp), allocatable :: realvec( : )
real(dfp), allocatable :: realvec2( : )
intvec = [ 5, 4, 3, 2, 1 ]
realvec = [ 8, 9, 12, 3, 2 ]
realvec2 = [ 8, 9, 12, 3, 2 ]
call QuickSort( &
& vect1=intvec, &
& vect2=realvec, &
& vect3=realvec2, &
& low=1, &
& high=SIZE(intvec) )
call display( intvec, "intvec = ", advance="NO" )
call display( realvec, "realvec= ", advance="NO" )
call display( realvec2, "realvec2= ", advance="YES" )
See results
intvec = , realvec= , realvec2=
---------, ---------, ----------
1 , 2.0000 , 2.0000
2 , 3.0000 , 3.0000
3 , 12.0000 , 12.0000
4 , 9.0000 , 9.0000
5 , 8.0000 , 8.0000
intvec = [ 5, 4, 3, 2, 1 ]
realvec = [ 8, 9, 12, 3, 2 ]
realvec2 = [ 8, 9, 12, 3, 2 ]
call QuickSort( &
& vect1=realvec, &
& vect2=intvec, &
& vect3=realvec2, &
& low=1, &
& high=SIZE(intvec) )
call display( intvec, "intvec = ", advance="NO" )
call display( realvec, "realvec= ", advance="NO" )
call display( realvec2, "realvec2= ", advance="YES" )
See results
intvec = , realvec= , realvec2=
---------, ---------, ----------
1 , 2.0000 , 2.0000
2 , 3.0000 , 3.0000
5 , 8.0000 , 8.0000
4 , 9.0000 , 9.0000
3 , 12.0000 , 12.0000
end program main
Interface 4
- ܀ QuickSort(vect1, vect2, vect3, vect4, low, high)
- ️܀ See example
- ↢
INTERFACE
MODULE PURE RECURSIVE SUBROUTINE QuickSort(vect1, vect2, vect3, &
& vect4, low, high)
INTEGER(I4B)| REAL( DFP ), DIMENSION(:), INTENT(INOUT) :: vect1, vect2, vect3, vect4
INTEGER(I4B), INTENT(IN) :: low, high
END SUBROUTINE QuickSort
END INTERFACE
In this interface vect1
, vect2
, vect3
, and vect4
can be a one dimensional fortran array of
Int32
orI4B
4 byte integersReal(DFP)
default floating point, usuallyReal64
In this example we test QuickSort method.
CALL QuickSort(v1,v2,v3,v4)
PROGRAM main
USE easifemBase
integer(i4b), allocatable :: i1(:),i2(:),i3(:), i4(:)
real( dfp ), allocatable :: r1(:),r2(:),r3(:),r4(:)
integer(i4b) :: first, last
i1 = [5,2,1,6,3,4]
i2 = i1
i3 = i1
i4 = i1
first=1; last=size(i1)
call QuickSort(i1,i2,i3,i4,first,last)
call display(i1, "i1=", orient="col", advance="NO")
call display(i2, "i2=", orient="col", advance="NO")
call display(i3, "i3=", orient="col", advance="NO")
call display(i4, "i4=", orient="col", advance="YES")
See results
i1=, i2=, i3=, i4=
---, ---, ---, ---
1 , 1 , 1 , 1
2 , 2 , 2 , 2
3 , 3 , 3 , 3
4 , 4 , 4 , 4
5 , 5 , 5 , 5
6 , 6 , 6 , 6
r4 = [1,2,3,2,2]
i1 = [0,1,0,0,0]
i2 = [0,0,1,0,0]
i3 = [0,0,0,1,1]
first=1; last=size(i1)
call QuickSort(i1,i2,i3,r4,first,last)
call blanklines(nol=2)
call display( "test-2" )
call display(i1, "i1=", orient="col", advance="NO")
call display(i2, "i2=", orient="col", advance="NO")
call display(i3, "i3=", orient="col", advance="NO")
call display(r4, "r4=", orient="col", advance="YES")
See results
i1=, i2=, i3=, r4=
---, ---, ---, -------
0 , 0 , 0 , 1.00000
0 , 0 , 1 , 2.00000
0 , 1 , 0 , 3.00000
0 , 0 , 1 , 2.00000
1 , 0 , 0 , 2.00000
END PROGRAM main