ユーマちゃんのブログ

質問・要望はTwitterもしくはコメントで。返信はTwitterで。

Programming Report9

program sele
implicit none
integer,parameter::max=100000
integer::i,j,n=0,status=0
integer:: a(max),b(max),d

!Here, open two files and named each of them like 10 and 20.
open(10,file="unsort.txt");open(20,file="sort.txt")

!Do loop until the unsort.txt becomes empty.
do while (status ==0)
n = n+1
!Here, we get a(1),...a(n) from unsort.txt.
read(10,*,IOSTAT=status) a(n)

end do

n = n-1;close(10); b = a

do i = n-1,2,-1 !If N=5, i = 4.3.2.
do j = 1,i !If N=5(like above), j=1~4,1~3,1~2. This is just same as Bubble sort.
if (b(j)>b(j+1)) then !Exchange if the previous value is bigger than next one.
d = b(j+1);b(j+1) =b(j);b(j) =d !Exchange process.
end if
end do
end do

do i = 1, n
write(*,'(i3)') b(i) !Write on the screen.
write(20,*) b(i) !Write on sorted.txt here.
end do

close(20)

end program sele