C Programming Code Examples C > Sorting Searching Code Examples C Program to Implement BogoSort in an Integer Array C Program to Implement BogoSort in an Integer Array #include <stdio.h> #include <stdlib.h> #define size 7 /* Function Prototypes */ int is_sorted(int *, int); void shuffle(int *, int); void bogosort(int *, int); int main() { int numbers[size]; int j; printf("Enter the elements of array:"); for (j = 0; j < size;j++) { scanf("%d", &numbers[j]); } bogosort(numbers, size); printf("The array after sorting is:"); for (j = 0;j < size;j++) { printf("%d\n", numbers[j]); } printf("\n"); } /* Code to check if the array is sorted or not */ int is_sorted(int *a, int n) { while (--n >= 1) { if (a[n] < a[n - 1]) { return 0; } } return 1; } /* Code to shuffle the array elements */ void shuffle(int *a, int n) { int j, t, temp; for (j = 0;j < n;j++) { t = a[j]; temp = rand() % n; /* Shuffles the given array using Random function */ a[j] = a[temp]; a[temp] = t; } } /* Code to check if the array is sorted or not and if not sorted calls the shuffle function to shuffle the array elements */ void bogosort(int *a, int n) { while (!is_sorted(a, n)) { shuffle(a, n); } }