program basic_sort implicit none integer i, N_max, N, M, N_total, yes_no parameter(N_max=2000) real A(N_max) character*3 switch character*20 filename write(*,*) 'Which file name has your data ? (No more than 20 chrs. ^, please.)' read(*,*) filename write(*,*) 'How many number are there in the file ?' write(*,*) 'Less than',N_max,'numbers, please' read(*,*) N_total if(N_total.gt.N_max) then write(*,*) 'Please change your progeam to make N_total larger.' write(*,*) 'The code will stop now, sorry.' stop end if open(unit=10, file=filename, status='OLD') do i=1,N_total read(10,*) A(i) end do do M=1, N_total-1 N = N_total - M + 1 call pick_maxmin(N, A, 'MAX') end do write(*,*) 'Sorting completed, the numbers from big to small are:' do i=1,N_total write(*,*) A(N_total + 1 - i) end do 200 write(*,*) 'Do you want to replace/overwrite the original file ?' write(*,*) '1 mean YES, 0 means NO.' read(*,*) yes_no if (yes_no .eq. 1) then close (10) open(unit=10, file=filename, status='OLD') do i =1, N_total write(*,*) A(i) end do write(*,*) 'Sorted numbers written in file in assending order.' else goto 200 end if close(10) end subroutine pick_maxmin(N,A,switch) C C This subroutine reads in an array A(N) and find its Max (or Min) number to C put it in A(N) (or A(1)), the rest of numbers occupie the rest N-1 positions C in the array and are unchanged in their order. C implicit none integer i,N real A(*), winner, losser character*3 switch if(N.lt.2) then write(*,*) 'Need at least two number to compare, please check.' stop end if if (switch.eq.'MAX') then do i=1,N-1 if (A(i).gt.A(N)) then winner = A(i) losser = A(N) else winner = A(N) losser = A(i) end if A(N) = winner A(i) = losser end do end if if (switch.eq.'MIN') then do i=2,N if (A(i).lt.A(1)) then winner = A(i) losser = A(1) else winner = A(1) losser = A(i) end if A(1) = winner A(i) = losser end do end if return end