Created
September 3, 2014 21:24
-
-
Save allthetime/dbb8ddddc345d20d4cda to your computer and use it in GitHub Desktop.
ruby 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(arr) | |
n = arr.length-1 | |
loop do | |
swapped = false | |
(1..n).each do |i| | |
if arr[i-1] > arr[i] then | |
remember = arr[i] | |
arr[i] = arr[i-1] | |
arr[i-1] = remember | |
swapped = true | |
end | |
end | |
print arr | |
puts | |
break if swapped == false | |
end | |
end | |
bubble([4,5,1,3,6,7,1,3]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment