Created
November 19, 2014 20:35
-
-
Save PatrickTulskie/696f56e11e73f54cd7c6 to your computer and use it in GitHub Desktop.
Shotgun sort in Ruby
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
class Array | |
def shotgun_sort | |
sorted = false | |
arry = self.dup | |
iteration = 0 | |
while !sorted do | |
puts "Attempt #{iteration += 1}" if $debug | |
arry.shuffle! | |
sorted = arry.each_cons(2).all? { |a, b| (a <=> b) <= 0 } | |
end | |
return arry | |
end | |
end | |
$debug = true | |
numbers = [1, 10, 5, 2, 20, 3, 6] | |
puts numbers.shotgun_sort.inspect |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment