-
-
Save anonymous/926f6bcc6e66c4f3a35d2702c26c4e9d to your computer and use it in GitHub Desktop.
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
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 |
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
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