BogoSort also known as permutation sort, stupid sort, slow sort, shotgun sort or monkey sort is a particularly ineffective algorithm based on generate and test paradigm. The algorithm successively generates permutations of its input until it finds one that is sorted.(Wiki)
For example, if bogosort is used to sort a deck of cards, it would consist of checking if the deck were in order, and if it were not, one would throw the deck into the air, pick the cards up at random, and repeat the process until the deck is sorted.
while not Sorted(list) do shuffle (list) done
Let us consider an example array ( 3 2 5 1 0 4 )
4 5 0 3 2 1 (1st shuffling)
4 1 3 2 5 0 (2ndshuffling)
1 0 3 2 5 4 (3rd shuffling)
3 1 0 2 4 5 (4th shuffling)
1 4 5 0 3 2 (5th shuffling)
0 1 2 3 4 5 (nth shuffling)—— Sorted Array
Here, n is unknown because algorithm doesn’t known in which step the resultant permutation will come out to be sorted.
Sorted array : 0 1 2 3 4 5
- Worst Case : O(∞) (since this algorithm has no upper bound)
- Average Case: O(n*n!)
- Best Case : O(n)(when array given is already sorted)
Auxiliary Space : O(1)
This article is contributed by Rahul Agrawal . If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- C++ Program for BogoSort or Permutation Sort
- Java Program for BogoSort or Permutation Sort
- Sort decreasing permutation of N using triple swaps
- Find a permutation that causes worst case of Merge Sort
- Sort permutation of N natural numbers using triple cyclic right swaps
- Minimum number of adjacent swaps required to convert a permutation to another permutation by given condition
- Minimum number of given operations required to convert a permutation into an identity permutation
- Comparison among Bubble Sort, Selection Sort and Insertion Sort
- Why Quick Sort preferred for Arrays and Merge Sort for Linked Lists?
- Add elements in start to sort the array | Variation of Stalin Sort
- Insertion sort to sort even and odd positioned elements in different orders
- Program to sort an array of strings using Selection Sort
- Bucket Sort To Sort an Array with Negative Numbers
- Serial Sort v/s Parallel Sort in Java
- Odd Even Transposition Sort / Brick Sort using pthreads
- Sort an Array which contain 1 to N values in O(N) using Cycle Sort
- Java Program for Odd-Even Sort / Brick Sort
- Quick Sort vs Merge Sort
- Merge Sort vs. Insertion Sort
- sort() vs. partial_sort() vs. nth_element() + sort() in C++ STL
Improved By : DrRoot_