From 1176079781f6f91f1bbf7bc478681e9c1990d2eb Mon Sep 17 00:00:00 2001 From: Chetan Kaushik Date: Fri, 5 Aug 2016 23:34:35 +0530 Subject: [PATCH] Added BogoSort --- BogoSort.c | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 BogoSort.c diff --git a/BogoSort.c b/BogoSort.c new file mode 100644 index 00000000..8ad36b40 --- /dev/null +++ b/BogoSort.c @@ -0,0 +1,40 @@ +#include +#include +#include + +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"); +}