Skip to content

Bogosort in c #301

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
72 changes: 72 additions & 0 deletions bogosort.c
Original file line number Diff line number Diff line change
@@ -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 <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <time.h>

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]);
}
}