Skip to content

Instantly share code, notes, and snippets.

@cciollaro
Created April 14, 2014 23:21
Show Gist options
  • Save cciollaro/10689616 to your computer and use it in GitHub Desktop.
Save cciollaro/10689616 to your computer and use it in GitHub Desktop.
cycle sort
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
@cciollaro
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment