TheAlgorithms-Ruby/sorting/bogo_sort.rb
2020-11-03 19:05:15 +09:00

18 lines
434 B
Ruby

class Array
def sorted?
### goes thru array and checks if all elements are in order
for i in 1...self.length
return false if self[i-1] > self[i]
end
return true
end
def bogosort
### randomly shuffles until sorted
self.shuffle! until self.sorted?
return self #return sorted array
end
end
puts "Enter a list of numbers separated by space"
str = gets.chomp.split('')
puts str.bogosort.join('')