Related Articles

# BogoSort or Permutation Sort

• Difficulty Level : Easy
• Last Updated : 08 Jun, 2021

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.

PseudoCode:

```while not Sorted(list) do
shuffle (list)
done```

Example:
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.

## C++

 `// C++ implementation of Bogo Sort``#include``using` `namespace` `std;` `// To check if array is sorted or not``bool` `isSorted(``int` `a[], ``int` `n)``{``    ``while` `( --n > 1 )``        ``if` `(a[n] < a[n-1])``            ``return` `false``;``    ``return` `true``;``}` `// To generate permutation of the array``void` `shuffle(``int` `a[], ``int` `n)``{``    ``for` `(``int` `i=0; i < n; i++)``        ``swap(a[i], a[``rand``()%n]);``}` `// Sorts array a[0..n-1] using Bogo sort``void` `bogosort(``int` `a[], ``int` `n)``{``    ``// if array is not sorted then shuffle``    ``// the array again``    ``while` `( !isSorted(a, n) )``        ``shuffle(a, n);``}` `// prints the array``void` `printArray(``int` `a[], ``int` `n)``{``    ``for` `(``int` `i=0; i

## Java

 `// Java Program to implement BogoSort``public` `class` `BogoSort``{``    ``// Sorts array a[0..n-1] using Bogo sort``    ``void` `bogoSort(``int``[] a)``    ``{``        ``// if array is not sorted then shuffle the``        ``// array again``        ``while` `(isSorted(a) == ``false``)``            ``shuffle(a);``    ``}` `    ``// To generate permutation of the array``    ``void` `shuffle(``int``[] a)``    ``{``         ``// Math.random() returns a double positive``         ``// value, greater than or equal to 0.0 and``         ``// less than 1.0.``         ``for` `(``int` `i=``1``; i <= n; i++)``             ``swap(a, i, (``int``)(Math.random()*i));``    ``}` `    ``// Swapping 2 elements``    ``void` `swap(``int``[] a, ``int` `i, ``int` `j)``    ``{``        ``int` `temp = a[i];``        ``a[i] = a[j];``        ``a[j] = temp;``    ``}` `    ``// To check if array is sorted or not``    ``boolean` `isSorted(``int``[] a)``    ``{``        ``for` `(``int` `i=``1``; i

## Python

 `# Python program for implementation of Bogo Sort``import` `random` `# Sorts array a[0..n-1] using Bogo sort``def` `bogoSort(a):``    ``n ``=` `len``(a)``    ``while` `(is_sorted(a)``=``=` `False``):``        ``shuffle(a)` `# To check if array is sorted or not``def` `is_sorted(a):``    ``n ``=` `len``(a)``    ``for` `i ``in` `range``(``0``, n``-``1``):``        ``if` `(a[i] > a[i``+``1``] ):``            ``return` `False``    ``return` `True` `# To generate permutation of the array``def` `shuffle(a):``    ``n ``=` `len``(a)``    ``for` `i ``in` `range` `(``0``,n):``        ``r ``=` `random.randint(``0``,n``-``1``)``        ``a[i], a[r] ``=` `a[r], a[i]` `# Driver code to test above``a ``=` `[``3``, ``2``, ``4``, ``1``, ``0``, ``5``]``bogoSort(a)``print``(``"Sorted array :"``)``for` `i ``in` `range``(``len``(a)):``    ``print` `(``"%d"` `%``a[i]),`

## C#

 `// C# implementation of Bogo Sort``using` `System;``  ` `class` `GFG``{``    ``// To Swap two given numbers``    ``static` `void` `Swap(``ref` `T lhs, ``ref` `T rhs)``    ``{``        ``T temp;``        ``temp = lhs;``        ``lhs = rhs;``        ``rhs = temp;``    ``}``    ` `    ``// To check if array is sorted or not``    ``public` `static` `bool` `isSorted(``int``[] a, ``int` `n)``    ``{``        ``int` `i = 0;``        ``while``(ia[i+1])``                ``return` `false``;``            ``i++;``        ``}``        ``return` `true``;``    ``}``      ` `    ``// To generate permutation of the array``    ``public` `static` `void` `shuffle(``int``[] a, ``int` `n)``    ``{``        ``Random rnd = ``new` `Random();``        ``for` `(``int` `i=0; i < n; i++)``            ``Swap(``ref` `a[i], ``ref` `a[rnd.Next(0,n)]);``    ``}``      ` `    ``// Sorts array a[0..n-1] using Bogo sort``    ``public` `static` `void` `bogosort(``int``[] a, ``int` `n)``    ``{``        ``// if array is not sorted then shuffle``        ``// the array again``        ``while` `( !isSorted(a, n) )``            ``shuffle(a, n);``    ``}``      ` `    ``// prints the array``    ``public` `static` `void` `printArray(``int``[] a, ``int` `n)``    ``{``        ``for` `(``int` `i=0; i

Output:

```Sorted array :
0 1 2 3 4 5 ```

Time Complexity:

• 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 write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.