! initialization e.g. inx=(/(j,j=1,150)/)  is required in the calling routine
module qsort_m
      private
      public myqsort, mymedian
      interface myqsort
        module procedure imyqsorti, smyqsorti, dmyqsorti, imyqsort, smyqsort, dmyqsort
      end interface myqsort
      
      interface mymedian
        module procedure imymedian, smymedian, dmymedian
      end interface mymedian
      
contains
      
      SUBROUTINE imyQsorti(N,ARR,inx)
! sort arr according to size, ascendingly. inx is passively sorted with arr.
      implicit none
      integer, intent(in) :: n
      integer, intent(inout) :: arr(n)
      integer, intent(inout) :: inx(n)
! local variables
      integer a      
      include 'include_myqsort.f90'
      END subroutine imyqsorti

      SUBROUTINE imyQsort(N,ARR)
! sort arr according to size, ascendingly
      implicit none
      integer, intent(in) :: n
      integer, intent(inout) :: arr(n)
! local variables
      integer a     
      include 'include_myqsort_noinx.f90'     
      END subroutine imyqsort

   
      SUBROUTINE smyQsorti(N,ARR,inx)
! sort arr according to size, ascendingly. inx is passively sorted with arr.
      implicit none
      integer, intent(in) :: n
      real, intent(inout) :: arr(n)
      integer, intent(inout) :: inx(n)
! local variables
      real a
      include 'include_myqsort.f90'
      END subroutine smyqsorti
      
      SUBROUTINE dmyQsorti(N,ARR,inx)
! sort arr according to size. inx is passively sorted with arr.
      implicit none
      integer, intent(in) :: n
      double precision, intent(inout) :: arr(n)
      integer, intent(inout) :: inx(n)
! local variables
      double precision a
      include 'include_myqsort.f90'
      END subroutine dmyqsorti
      
      SUBROUTINE smyQsort(N,ARR)
! sort arr according to size, ascendingly
      implicit none
      integer, intent(in) :: n
      real, intent(inout) :: arr(n)
! local variables
      real a
      include 'include_myqsort_noinx.f90'
      END subroutine smyqsort
      
      SUBROUTINE dmyQsort(N,ARR)
! sort arr according to size. 
      implicit none
      integer, intent(in) :: n
      double precision, intent(inout) :: arr(n)
! local variables
      double precision a
      include 'include_myqsort_noinx.f90'
      END subroutine dmyqsort
      
!***********************************************************************
!*****       FINDS THE Kth SMALLEST OF N ITEMS IN ARRAY A
!*****       BY THE ALGORITHM OF C.A.R. HOARE                    11-1995
!***********************************************************************
!
!   N    - NUMBER OF ITEMS IN ARRAY A                            (GIVEN)
!   K    - NUMBER OF THE Kth SMALLEST OF N ITEMS IN ARRAY A      (GIVEN)
!   A    - INTEGER*4 ARRAY OF ITEMS. ON RETURN, THE Kth        (CHANGED)
!          SMALLEST ITEM IS FOUND IN A(K);i.e.
!          A(L)<=A(K) for 1<=L<=K  and A(L)>=A(K) for K<=L<=N
!
!***********************************************************************
      SUBROUTINE imymedian(N,K,A)
      IMPLICIT  NONE
      INTEGER A(n),X,W
      INCLUDE 'include_mymedian.f90'
      END SUBROUTINE imymedian
      
      SUBROUTINE smymedian(N,K,A)
      IMPLICIT  NONE
      REAL A(n),X,W
      INCLUDE 'include_mymedian.f90'
      END SUBROUTINE smymedian
      
      SUBROUTINE dmymedian(N,K,A)
      IMPLICIT  NONE
      DOUBLE PRECISION A(n),X,W
      INCLUDE 'include_mymedian.f90'
      END SUBROUTINE dmymedian
end module qsort_m
