Created
April 14, 2014 23:21
-
-
Save cciollaro/10689616 to your computer and use it in GitHub Desktop.
cycle 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
blocks = (0..8).to_a.shuffle | |
puts blocks.inspect | |
i = 0 | |
while i < 8 | |
until i == blocks[i] | |
j = blocks[i] | |
#swap blocks[i] with blocks[blocks[i]] | |
blocks[i], blocks[j] = blocks[j], blocks[i] | |
end | |
i += 1 | |
end | |
puts blocks.inspect |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
cycle sort implemented in ruby
more info here: https://en.wikipedia.org/wiki/Cycle_sort
I ended up using similar code to write a naive defragmenter for a school project because of the minimal number of writes