第一章 排序
1. 桶算法(简单):
缺点:太占空间,只能对数本身排序,无用。
C:
int a[11], i,j,t;
for (i= 0;i <= 0; i++){
...
}
ruby:
本质上for...in是语法糖,ruby translates it into sht like: each do..end
You can use for to iterate over any Object that reponds to the method each, such as Array or a Range.
a = []
for i in 0..10 do
a[i] = 0
end
print a
puts "\n" #屏幕上换行
a[2] = 1
a[3] = 1
a[5] = 2
a[8] = 1
# 正序排列
for i in 0..10 do
j = 1
while j <= a[i]
print i
j += 1
end
end
puts "\n"
# 反序排列
i = 10
while i >=0
j = 1
while j <= a[i]
print i
j += 1
end
i -= 1
end
puts "\n"
# 结果是:
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
# 23558
# 85532
2.冒泡排序