Bogo Sort 💩
Bogo sort is a humorous and inefficient sorting algorithm. It is based on the idea of randomly shuffling elements of an array until they are in the correct order.
It is not considered as a practical sorting algorithm due to its extremely slow-running time, even for small input sizes. It's mostly used as a demonstration of the importance of using efficient algorithms.
Example in TypeScript
This function takes an array as an argument and checks whether the array is sorted using the isSorted(arr)
function. If it's not sorted, it shuffles the array using the shuffle(arr)
function.
Usage
It should be noted that the bogo sort is impractical to be used as a sorting algorithm. Due to its extremely high running time, The time complexity of this algorithm is O(∞)
on average and O(n!)
in the worst case, where n
is the number of elements in the array. Even for small input sizes, it takes a very long time to sort the array, and it's most likely will never complete. It's a good demonstration for why the efficient sorting algorithms are important and the time complexity of an algorithm should be considered when choosing the algorithm.
Last updated