bogo sort Algorithm
The bogo sort algorithm, also known as stupid sort, random sort, or monkey sort, is a highly ineffective and inefficient sorting algorithm based on the principle of pure randomness. It is primarily used for educational purposes or as a joke due to its lack of practicality in real-world situations. The algorithm works by randomly shuffling or permuting the elements of a list until the list is sorted.
The bogo sort algorithm has a worst-case time complexity of O(n!) or O((n+1)!) depending on the implementation, which makes it highly impractical for use in any realistic scenario. In the best case, the algorithm may sort the list in just one iteration, but the probability of that happening is extremely low, especially as the size of the list grows. Due to its overall inefficiency and lack of any redeeming qualities, the bogo sort algorithm serves as a reminder of what not to do when designing a sorting algorithm for practical applications.
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 seprated by space"
str = gets.chomp.split('')
puts str.bogosort.join('')