Created
May 3, 2014 03:32
-
-
Save bgmarx/01b2fc5025e873952fe8 to your computer and use it in GitHub Desktop.
bubble sort
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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