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.


#include<bits/stdc++.h> 
using namespace std; 
  

bool isSorted(int a[], int n) 
{ 
    while ( --n > 1 ) 
        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++) 
        printf("%d ", a[i]); 
    printf("\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; 
} 

LANGUAGE:

DARK MODE: