C Program to Implement BogoSort

«
»
This C Program Implements BogoSort in an Integer Array.

Here is source code of the C Program to Implement BogoSort in an Integer Array. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Implement BogoSort in an Integer Array
  3.  */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6.  
  7. #define size 7
  8. /* Function Prototypes */
  9.  
  10. int is_sorted(int *, int);
  11. void shuffle(int *, int); 
  12. void bogosort(int *, int);
  13.  
  14. int main()
  15. {
  16.     int numbers[size];
  17.     int i;
  18.  
  19.     printf("Enter the elements of array:");
  20.     for (i = 0; i < size;i++)
  21.     {
  22.         scanf("%d", &numbers[i]);
  23.     }
  24.     bogosort(numbers, size);
  25.     printf("The array after sorting is:");
  26.     for (i = 0;i < size;i++)
  27.     {
  28.         printf("%d\n", numbers[i]);
  29.     }
  30.     printf("\n");
  31. }
  32.  
  33. /* Code to check if the array is sorted or not */
  34. int is_sorted(int *a, int n)
  35. {
  36.     while (--n >= 1)
  37.     {
  38.         if (a[n] < a[n - 1])
  39.         {
  40.             return 0;
  41.           }
  42.     }
  43.       return 1;
  44. }
  45.  
  46. /* Code to shuffle the array elements */
  47. void shuffle(int *a, int n)
  48. {
  49.     int i, t, temp;
  50.     for (i = 0;i < n;i++)
  51.     {
  52.         t = a[i];
  53.         temp = rand() % n;    /* Shuffles the given array using Random function */
  54.         a[i] = a[temp];
  55.         a[temp] = t;
  56.     }
  57. }
  58.  
  59. /* Code to check if the array is sorted or not and if not sorted calls the shuffle function to shuffle the array elements */
  60. void bogosort(int *a, int n)
  61. {
  62.     while (!is_sorted(a, n))
  63.     {
  64.         shuffle(a, n);
  65.     }
  66. }

advertisement
$ cc bogo_sort.c
Average case:
$ a.out
Enter the elements of array:56
34
96
26
08
87
36
The array after sorting is:8
26
34
36
56
87
96
 
Best case:
$ a.out
Enter the elements of array:12
23
34
45
56
67
78
The array after sorting is:12
23
34
45
56
67
78
 
Worst case:
$ a.out
Enter the elements of array:984
38
983
389
37
596
483
The array after sorting is:37
38
389
483
596
983
984

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.