Skip to content

Instantly share code, notes, and snippets.

@allthetime
Created September 3, 2014 21:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save allthetime/dbb8ddddc345d20d4cda to your computer and use it in GitHub Desktop.
Save allthetime/dbb8ddddc345d20d4cda to your computer and use it in GitHub Desktop.
ruby bubble sort
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