Last active
October 13, 2015 21:08
-
-
Save nickserv/4256138 to your computer and use it in GitHub Desktop.
Bogo 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 in_order? | |
(0..length-2).none? { |i| self[i] > self[i+1] } | |
end | |
def bogo_sort | |
copy = self.dup | |
copy.shuffle! until copy.in_order? | |
copy | |
end | |
end | |
test_array = [] | |
5.times { test_array << rand(10) } | |
p test_array.bogo_sort |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment