Skip to content

Instantly share code, notes, and snippets.

@PatrickTulskie
Created November 19, 2014 20:35
Show Gist options
  • Save PatrickTulskie/696f56e11e73f54cd7c6 to your computer and use it in GitHub Desktop.
Save PatrickTulskie/696f56e11e73f54cd7c6 to your computer and use it in GitHub Desktop.
Shotgun sort in Ruby
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