首页 > 代码库 > Ruby实现冒泡排序

Ruby实现冒泡排序

 1 def bubble_sort(array)
 2     return array if array.size < 2
 3 
 4     (array.size-2).downto(0) do |i|
 5         (0..i).each do |j|
 6             array[j],array[j+1] = array[j+1],array[j] if array[j] >= array[j+1]
 7         end
 8     end
 9 
10     return array
11 end
12 
13 a = [12,42,1,25,32,63,18]
14 p bubble_sort(a)