BogoSort also known as permutation sort, stupid sort, slow sort, shotgun sort or monkey sort is a particularly ineffective algorithm one person can ever imagine. It is 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.
Algorithm:
Bogo sort uses 2 steps to sort elements of the array.
1. It throws the number randomly.
2. Check whether the number is sorted or not.
3. If sorted then return the sorted array.
4. Otherwise it again generate another randomization of the numbers until the array 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 know in which step the resultant permutation will come out to be sorted.
C++
#include <bits/stdc++.h>
using namespace std;
bool isSorted( int a[], int n)
{
while (--n > 0)
if (a[n] < a[n - 1])
return false ;
return true ;
}
void shuffle( int a[], int n)
{
for ( int i = 0; i < n; i++)
swap(a[i], a[ rand () % n]);
}
void bogosort( int a[], int n)
{
while (!isSorted(a, n))
shuffle(a, n);
}
void printArray( int a[], int n)
{
for ( int i = 0; i < n; i++)
cout << a[i] << " " ;
cout << "\n" ;
}
int main()
{
int a[] = { 3, 2, 5, 1, 0, 4 };
int n = sizeof a / sizeof a[0];
bogosort(a, n);
printf ( "Sorted array :\n" );
printArray(a, n);
return 0;
}
|
Java
public class BogoSort {
void bogoSort( int [] a)
{
while (isSorted(a) == false )
shuffle(a);
}
void shuffle( int [] a)
{
for ( int i = 1 ; i < a.length; i++)
swap(a, i, ( int )(Math.random() * i));
}
void swap( int [] a, int i, int j)
{
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
boolean isSorted( int [] a)
{
for ( int i = 1 ; i < a.length; i++)
if (a[i] < a[i - 1 ])
return false ;
return true ;
}
void printArray( int [] arr)
{
for ( int i = 0 ; i < arr.length; i++)
System.out.print(arr[i] + " " );
System.out.println();
}
public static void main(String[] args)
{
int [] a = { 3 , 2 , 5 , 1 , 0 , 4 };
BogoSort ob = new BogoSort();
ob.bogoSort(a);
System.out.print( "Sorted array: " );
ob.printArray(a);
}
}
|
Python
import random
def bogoSort(a):
n = len (a)
while (is_sorted(a) = = False ):
shuffle(a)
def is_sorted(a):
n = len (a)
for i in range ( 0 , n - 1 ):
if (a[i] > a[i + 1 ]):
return False
return True
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]
a = [ 3 , 2 , 4 , 1 , 0 , 5 ]
bogoSort(a)
print ( "Sorted array :" )
for i in range ( len (a)):
print ( "%d" % a[i]),
|
C#
using System;
class GFG {
static void Swap<T>( ref T lhs, ref T rhs)
{
T temp;
temp = lhs;
lhs = rhs;
rhs = temp;
}
public static bool isSorted( int [] a, int n)
{
int i = 0;
while (i < n - 1) {
if (a[i] > a[i + 1])
return false ;
i++;
}
return true ;
}
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)]);
}
public static void bogosort( int [] a, int n)
{
while (!isSorted(a, n))
shuffle(a, n);
}
public static void printArray( int [] a, int n)
{
for ( int i = 0; i < n; i++)
Console.Write(a[i] + " " );
Console.Write( "\n" );
}
static void Main()
{
int [] a = { 3, 2, 5, 1, 0, 4 };
int n = a.Length;
bogosort(a, n);
Console.Write( "Sorted array :\n" );
printArray(a, n);
}
}
|
Javascript
<script>
function isSorted(a, n){
for ( var i = 1; i < arr.length; i++)
if (a[i] < a[i-1])
return false ;
return true ;
}
function swap(arr, xp, yp){
var temp = arr[xp];
arr[xp] = arr[yp];
arr[yp] = temp;
}
function shuffle(a, n){
var i, j=n;
for (i=0; i < n; i++){
var ind = Math.floor(Math.random() * n);
swap(a, j-i-1, ind);
}
return a;
}
function bogosort(a, n){
while (!isSorted(a, n))
a = shuffle(a, n);
return a;
}
function printArray(arr, size){
var i;
for (i=0; i < size; i++)
document.write(arr[i]+ " " );
document.write( "\n" );
}
var arr = [3, 2, 5, 1, 0, 4];
var n = arr.length;
arr = bogosort(arr, n);
document.write( "Sorted array: \n" );
printArray(arr, n);
</script>
|
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)
Bozo Sort Algorithm:
Bozo sort is a variation of Bogo sort and is little more efficient than Bogo sort.
Check if the array is sorted or not.
Unlike bogo sort, if the list/array is not sorted then it picks only two items at random and swap them.
Then it checks if the array/list is sorted or not.
But like bogo sort, there is chance that it faces the same pseudo-random problems and it may never terminate.
It means it also has O(?) worst time complexity but its average case complexity is better than Bogo sort.
Question:
1. Tell the other names of Bogo sort?
Ans. Bogo sort has many other names like, permutation sort, slow sort, shotgun sort, stupid sort, bozo sort, blort sort, monkey sort, random sort or drunk man sort.
2. Why Bogo sort is called stupid sort, bozo sort, blort sort, monkey sort, random sort or drunk man sort?
Ans. Complexity of Bogo sort is O(?) since this algorithm has no upper bound. So, you can imagine how much inefficient this sorting algorithm is. That’s why for its tremendous inefficiency, it is jokingly called stupid sort, bozo sort, blort sort, monkey sort, random sort or drunk man sort.
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
11 Jul, 2023
Like Article
Save Article