PROGRAM Sorting
   IMPLICIT NONE
   INTEGER, PARAMETER        :: Max = 100
   INTEGER, DIMENSION(1:Max) :: InputData
   INTEGER                   :: ActualSize, I

   READ(*,*) ActualSize, (InputData(I), I = 1, ActualSize)
   WRITE(*,*) 'Input Array: '
   WRITE(*,*) (InputData(I), I = 1, ActualSize)
   
   CALL Sort(InputData, ActualSize)
   
   WRITE(*,*) 
   WRITE(*,*) 'Input Array: '
   WRITE(*,*) (InputData(I), I = 1, ActualSize)

CONTAINS
   INTEGER FUNCTION FindMinimum(x, Start, End)
      IMPLICIT NONE
      INTEGER, DIMENSION(:), INTENT(IN) :: x
      INTEGER, INTENT(IN)               :: Start, End
      INTEGER                           :: Minimum, Location, I

      Minimum = x(Start)
      Location = Start
      DO I = Start + 1, End
         IF (x(I) < Minimum) THEN
            Minimum = x(I)
            Location = I
         END IF
      END DO
      FindMinimum = Location
   END FUNCTION FindMinimum

   SUBROUTINE Swap(A, B)
      IMPLICIT NONE
      INTEGER, INTENT(INOUT) :: A, B
      INTEGER                :: Tmp

      Tmp = A
      A = B
      B = Tmp
   END SUBROUTINE Swap

  SUBROUTINE Sort(x, Size)
     IMPLICIT NONE
     INTEGER, DIMENSION(:), INTENT(INOUT) :: x
     INTEGER, INTENT(IN)                  :: Size
     INTEGER                              :: I, Location

     DO I = 1, Size - 1
        Location = FindMinimum(x, i, Size)
        CALL Swap(x(i), x(Location))
     END DO
  END SUBROUTINE Sort
END PROGRAM Sorting 

