# selection sort

Update:

close(15)這串是什麼意思阿

open (unit=15, file='score.dat', status='old', access='sequential')

Rating
• Jack
Lv 5

程式如下：（假設學生成績是儲存在文字檔 score.dat 內）

program main

integer s(10), i, j, im, it

open (unit=15, file='score.dat', status='old', access='sequential')

close(15)

write (*, "(10i3)") s

do i = 1, 9

im = i

do j = i+1, 10

if (s(j) < s(im)) im = j

end do

it = s(i)

s(i) = s(im)

s(im) = it

write (*, "(10i3)") s

end do

write (*, "(/'Highest score =', i3)") s(10)

write (*, "('Lowest score =', i3)") s(1)

write (*, "('Sorted score list: ', 10i3)") s

end

執行結果：

60 40 80 50 70 30 55 65 90 75

30 40 80 50 70 60 55 65 90 75

30 40 80 50 70 60 55 65 90 75

30 40 50 80 70 60 55 65 90 75

30 40 50 55 70 60 80 65 90 75

30 40 50 55 60 70 80 65 90 75

30 40 50 55 60 65 80 70 90 75

30 40 50 55 60 65 70 80 90 75

30 40 50 55 60 65 70 75 90 80

30 40 50 55 60 65 70 75 80 90

Highest score = 90

Lowest score = 30

Sorted score list: 30 40 50 55 60 65 70 75 80 90

為了證明這確實是用 selection sort 演算法，我把排序的中間過程也列印出來以供參考。當然你也可以只列印最後結果，不列印中間過程。

2009-06-11 18:36:07 補充：