From 17acee0e7e596c0d34f55bb8cf0b9d5432629006 Mon Sep 17 00:00:00 2001 From: Marco Date: Mon, 7 Apr 2025 17:11:36 +0200 Subject: [PATCH 1/2] Create bogosort --- bogosort.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 bogosort.c diff --git a/bogosort.c b/bogosort.c new file mode 100644 index 0000000..7c733ee --- /dev/null +++ b/bogosort.c @@ -0,0 +1,72 @@ +/* bogosort is a sorting algorithm based on the generate and test paradigm. The function successively generates + * permutations of its input until it finds one that is sorted. It is not considered useful for sorting, but may be + * used for educational purposes. Do not test it with large arrays */ + +#include +#include +#include +#include + +void bogosort(int * array, size_t size); +bool issorted(const int * array, size_t size); +void randomize(int * array, size_t size); +void printArray(const int * array, size_t size); + +int main(void) { + // example array + int array[] = { + 3, + 15, + 2, + 10 + }; + int size = sizeof(array) / sizeof( * array); + + srand(time(NULL)); + + // prints initial array + printf("Initial array:"); + printArray(array, size); + + // call function to sort the array + bogosort(array, size); + + // prints sorted array + printf("\nSorted array:"); + printArray(array, size); + + return 0; +} + +// bogosort +void bogosort(int * array, size_t size) { + while (!issorted(array, size)) { + randomize(array, size); + } +} + +// returns true if the array is sorted +bool issorted(const int * array, size_t size) { + for (size_t i = 0; i < size - 1; i++) { + if (array[i] > array[i + 1]) return false; + } + return true; +} + +// randomize array +void randomize(int * array, size_t size) { + int temp, random; + for (int i = 0; i < size; i++) { + random = (int)((double) rand() / ((double) RAND_MAX + 1) * (double) size); + temp = array[random]; + array[random] = array[i]; + array[i] = temp; + } +} + +// prints arrays +void printArray(const int * array, size_t size) { + for (size_t i = 0; i < size; i++) { + printf(" %d", array[i]); + } +} \ No newline at end of file From 66c439d213fe389f6a0f3c280878d8cd562344ff Mon Sep 17 00:00:00 2001 From: Marco Date: Mon, 7 Apr 2025 17:13:28 +0200 Subject: [PATCH 2/2] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 341baa9..1e3c6ca 100644 --- a/README.md +++ b/README.md @@ -105,6 +105,7 @@ These program are written in codeblocks ide for windows. These programs are not - [To check if a matrix is a sparse matrix or not](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/SparseMatrix_017.c) - [To calculate the Least Common Multiple](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/lcm.c) - [Lambda in C](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/lambda_in_c.c) +- [BogoSort](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/bogosort.c) # Contributing This is a personal learning project for me.