TheAlgorithms-Ruby/sorting/bogo_sort.rb
Vitor Oliveira e21120857d Clean up
2021-02-06 23:05:54 -08:00

21 lines
442 B
Ruby

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