Skip to content

Instantly share code, notes, and snippets.

@bgmarx
Created May 3, 2014 03:32
Show Gist options
  • Save bgmarx/01b2fc5025e873952fe8 to your computer and use it in GitHub Desktop.
Save bgmarx/01b2fc5025e873952fe8 to your computer and use it in GitHub Desktop.
bubble sort
def bubble_sort(list)
return list if list.size <= 1
swapped = true
while swapped
swapped = false
0.upto(list.size-2) do |i|
if(list[i] > list[i+1])
list[i + 1], list[i] = list[i], list[i+1]
swapped = true
end
end
end
print list
end
a = [4,3,9,12,4242,1,53634242,5,72]
puts bubble_sort(a)
# [1, 3, 4, 5, 9, 12, 72, 4242, 53634242]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment