Professional Documents
Culture Documents
Bubble Sort
Bubble Sort
05 Friday
def bubbleSort(a,n)
flag = true
while flag == true do
flag = false
(n-1).downto(1) do |j|
if a[j] < a[j-1] then
tmp = a[j]
a[j] = a[j-1]
a[j-1] = tmp
flag = true
end
end
end
return a
end
a=[5,3,2,4,1]
p bubbleSort(a,5)
ruby bubblesort.rb
[1, 2, 3, 4, 5]