Skip to content

Instantly share code, notes, and snippets.

/bubble1.rb Secret

Created October 2, 2016 19:57
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 anonymous/926f6bcc6e66c4f3a35d2702c26c4e9d to your computer and use it in GitHub Desktop.
Save anonymous/926f6bcc6e66c4f3a35d2702c26c4e9d to your computer and use it in GitHub Desktop.
elems = 10.times.to_a.shuffle
p elems
loop do
changed = false
(0..(elems.size-2)).each do |i|
if elems[i] > elems[i+1]
elems[i], elems[i+1] = elems[i+1], elems[i]
changed = true
p elems
end
end
break unless changed
end
Sample output
[2, 7, 1, 6, 5, 0, 9, 3, 4, 8]
[2, 1, 7, 6, 5, 0, 9, 3, 4, 8]
[2, 1, 6, 7, 5, 0, 9, 3, 4, 8]
[2, 1, 6, 5, 7, 0, 9, 3, 4, 8]
[2, 1, 6, 5, 0, 7, 9, 3, 4, 8]
[2, 1, 6, 5, 0, 7, 3, 9, 4, 8]
[2, 1, 6, 5, 0, 7, 3, 4, 9, 8]
[2, 1, 6, 5, 0, 7, 3, 4, 8, 9]
[1, 2, 6, 5, 0, 7, 3, 4, 8, 9]
[1, 2, 5, 6, 0, 7, 3, 4, 8, 9]
[1, 2, 5, 0, 6, 7, 3, 4, 8, 9]
[1, 2, 5, 0, 6, 3, 7, 4, 8, 9]
[1, 2, 5, 0, 6, 3, 4, 7, 8, 9]
[1, 2, 0, 5, 6, 3, 4, 7, 8, 9]
[1, 2, 0, 5, 3, 6, 4, 7, 8, 9]
[1, 2, 0, 5, 3, 4, 6, 7, 8, 9]
[1, 0, 2, 5, 3, 4, 6, 7, 8, 9]
[1, 0, 2, 3, 5, 4, 6, 7, 8, 9]
[1, 0, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment