Bogo Sort Algorithm

The Bogo Sort Algorithm, also known as the "stupid sort" or "slow sort," is a highly inefficient sorting algorithm based on generating random permutations of a given input list until a correctly sorted list is produced. The algorithm's name is derived from the word "bogosort," a pun on "bogus," which means fake or false, highlighting its absurdity and inefficiency. This algorithm is primarily used as a humorous example of a sorting algorithm that should never be used in practical applications due to its incredibly poor performance. The Bogo Sort Algorithm works by repeatedly shuffling the input list randomly and checking whether the resulting arrangement is sorted. If the shuffled list is sorted, the algorithm terminates, and the sorted list is returned; otherwise, the process is repeated. The algorithm's time complexity is unbounded, with an average-case performance of O((n+1)!) for a list of length n, making it highly impractical for sorting even relatively small datasets. While Bogo Sort is often used to illustrate the worst-case scenario for sorting algorithms, it also provides an interesting contrast to more efficient and practical sorting methods such as Quick Sort, Merge Sort, and Bubble Sort.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

bool check_sorted(int *a, int n)
{
  while ( --n >= 1 ) {
    if ( a[n] < a[n-1] ) return false;
  }
  return true;
}

void shuffle(int *a, int n)
{
  int i, t, r;
  for(i=0; i < n; i++) {
    t = a[i];
    r = rand() % n;
    a[i] = a[r];
    a[r] = t;
  }
}

void sort(int *a, int n)
{
  while ( !check_sorted(a, n) ) shuffle(a, n);
}

int main()
{
  int numbers[6];
  int i;
  printf("Enter 6 numbers unsorted \n\n");
  for(i=0;i<6;i++){
  scanf("%d",&numbers[i]);
  }
  sort(numbers, 6);
  for (i=0; i < 6; i++) printf("%d ", numbers[i]);
  printf("\n");
}

LANGUAGE:

DARK MODE: