next up previous
Next: Sort-2 Up: Sample Programs Previous: Calc_Print_sin

Sort-1

      program SortData
      Implicit None
      Integer I, J, Position, num_grades
      Real Grade(1000), temp, Small

C             Open Files

      Open(unit=1,file='data.in',type='old')
      Open(unit=2,file='data.out',type='unknown')


C             Get Data

      Do i = 1, 1000000
         read(1,*,end=10) Grade(i)
      end do
   10 num_grades = i - 1

C             Sort Data

      Do i = 1 , num_grades - 1       ! start outer loop
         Small = Grade(i)             ! assume current element is smallest
         Position = i
         Do j = i+1, num_grades       ! consider remainder of array
            If (Grade(j) .LT. Small) Then  ! check if current element
               Small = Grade(j)            ! is smaller than the smallest
               Position = j                ! found thusfar
            end if
         end do 
         temp = Grade(Position)       ! switch smallest element
         Grade(Position) = Grade(i)   ! with current element
         Grade(i) = temp
      end do

C             Output Data

      Do i = 1,num_grades
         write(2,*) Grade(i)
      end do

      end



Russ Miller
Thu Sep 14 13:56:19 EDT 1995