!c*****   ELABORADO POR FERNANDO LAROCA


  Program  Ordena

    Implicit none


	Real*8                           :: X(10)

	Integer                          :: n=10

	Integer                          :: imax

	Integer                          :: i



	Call Entrada(n, X)

	do i = n, 2, -1

	   Call Maior(n, i, X, imax)

       Call Troca(n, i, X, imax)

	 enddo

	Call Saida(n, X)

  End Program


!C*************************************************************************
!C****     Entrada de dados
!C************************************************************************* 
  Subroutine Entrada(n, X)

    Implicit none


	Integer, intent(in)                    ::n   

	Real*8,  intent(out)                   :: X(n)

	Integer                                :: j


	open(7,file='dados.txt', form='formatted', status='old')

	do j = 1, n

	  read(7,*)  X(j)

	enddo

	close(7)

	Return

  End Subroutine

!C*************************************************************************
!C****     Procura maior valor e armazena indice em imax
!C*************************************************************************
  
  Subroutine Maior(n, i, X, imax)

    Implicit none

	Integer, intent(in)                   :: n

	Integer, intent(in)                   :: i

	Integer, intent(out)                  :: imax

	Real*8, intent(in)                    :: X(n)

	Integer                               :: j


	do j = 2, i

	  if( X(j).LT.X(j-1) ) imax = i

    enddo

	End Subroutine

!C*************************************************************************
!C****    Troca o valor maior pelo primeiro da lista
!C*************************************************************************

  Subroutine Troca(n, i, X, imax)

    Implicit none

	Integer, intent(in)                    :: n

	Integer, intent(in)                    :: i

	Integer, intent(in)                    :: imax

	Real*8, intent(inout)                  :: X(n)


	Real*8                                 :: aux


	aux = X(imax)

	X(imax) = X(i)

	X(i) = aux

  End Subroutine

!C*************************************************************************
!C****     Saida de dados
!C*************************************************************************
  Subroutine Saida(n, X)

    Implicit none

	Integer, intent(in)                      :: n

	Real*8, intent(in)                       :: X(n)

	Integer                                  :: j


	open(8,file='ordenado.txt', form='formatted')

	do j = 1, n

	  write(8,10)  X(j)

	enddo

10  format ( F8.2 )

	close(8)

  End Subroutine
