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)
时间: 2024-10-29 19:08:53