! Program Example from "Essential Fortran 90 & 95" by Loren P. Meissner
! Copyright 1996. Copying for sale requires permission from the author.
! Otherwise, distribution is permitted if these three lines are included.

  module H06M
    use Swap_Reals_M
    implicit none
    private
    public :: Selection_Sort
  contains   ! Subprogram part of module
    recursive subroutine Selection_Sort( Unsorted )   ! Module subprogram
      real, intent (in out), dimension(:) :: Unsorted   ! Assumed-shape array
      integer :: N
      integer, dimension(1) :: Loc
  ! start subroutine Selection_Sort
      N = size( Unsorted )
      if (N > 1) then   ! End recursion if only one element remains.
        Loc = maxloc( Unsorted )   ! Location of largest element
        call Swap( Unsorted(N), Unsorted(Loc(1)) )
        call Selection_Sort( Unsorted(1: N - 1) )   ! Recursion
      end if
      return
    end subroutine Selection_Sort
  end module H06M

