Skip to content

Instantly share code, notes, and snippets.

@yoshikischmitz
Last active August 29, 2015 13:55
Show Gist options
  • Save yoshikischmitz/8776830 to your computer and use it in GitHub Desktop.
Save yoshikischmitz/8776830 to your computer and use it in GitHub Desktop.
Bogosort
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