PROGRAM sortn

  IMPLICIT NONE
  INTEGER, DIMENSION(:), ALLOCATABLE :: nums
  INTEGER :: nvals, temp, j, k

!-- read in the data
  PRINT *,'Enter the number of values to be input'
  READ *, nvals
  ALLOCATE(nums(nvals))
  PRINT *, 'Type the INTEGERs each on a new line'
  DO j = 1,nvals
    READ *,nums(j)
  ENDDO

!-- echo the input data
  PRINT *, 'orig nums array = ',nums

!-- sort the numbers into ascending order of magnitude
  L1: DO j = 1,nvals-1
  L2:   DO k = j+1,nvals
           IF (nums(j) > nums(k)) THEN
             temp = nums(k)
             nums(k) = nums(j)
             nums(j) = temp
           ENDIF
        ENDDO L2
      ENDDO L1

!-- write the sorted list
  DO j = 1,nvals
    PRINT *,'rank ',j,' valus is ',nums(j)
  ENDDO

!-- DEALLOCATE the array
  DEALLOCATE(nums)

END PROGRAM sortn
