Advertisement
oaktree

Bogo Sort - Ruby

Mar 12th, 2016
132
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Array
  2.     def sorted?
  3.         ### goes thru array and checks if all elements are in order
  4.         for i in 1...self.length
  5.             return false if self[i-1] > self[i]
  6.         end
  7.         return true
  8.     end
  9.     def bogosort
  10.         ### randomly shuffles until sorted
  11.         self.shuffle! until self.sorted?
  12.         return self #return sorted array
  13.     end
  14. end
  15.  
  16. puts "give me a string"
  17. str = gets.chomp.split('')
  18. puts str.bogosort.join('')
Advertisement
RAW Paste Data Copied
Advertisement