5人の学生の身長を ソート
明解C言語 入門編 > 12. 構造体 >
5人の学生の身長を ソート
Ruby
NINSU = 5 def sort(data, n) k = n - 1 while (k >= 0) j = -1 for i in 1..k if (data[i - 1] > data[i]) j = i - 1 data[i], data[j] = data[j], data[i] end end k = j end end height = [178, 175, 173, 165, 179] puts "ソート前:" height.each_with_index do |h, i| printf("%2d:%4d\n", i + 1, h) end puts "" sort(height, NINSU) puts "ソート後:" height.each_with_index do |h, i| printf("%2d:%4d\n", i + 1, h) end
実行結果
L:\>ruby l:\lesson_12_088.rb
メ[ト前:
1: 178
2: 175
3: 173
4: 165
5: 179メ[ト後:
1: 165
2: 173
3: 175
4: 178
5: 179