Last active
August 29, 2015 13:55
-
-
Save yoshikischmitz/8776830 to your computer and use it in GitHub Desktop.
Bogosort
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 is_sorted? | |
self.each_with_index do |curr_element, index| | |
next_element = self[index + 1] | |
unless next_element == nil || curr_element < next_element | |
return false | |
end | |
end | |
return true | |
end | |
def bogosort | |
sorted = false | |
until sorted == true | |
self.shuffle! | |
sorted = self.is_sorted? | |
end | |
self | |
end | |
end | |
array = Array.new(4).map {|x| x = rand(0..999) } | |
array.shuffle! if array.sort == array | |
puts "Unsorted array is:\t#{array}" | |
puts "Sorted array is:\t\t#{array.bogosort}" | |
puts "#{array}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment