diff --git a/bubblesort.c b/bubblesort.c new file mode 100644 index 0000000..9c3b8ca --- /dev/null +++ b/bubblesort.c @@ -0,0 +1,40 @@ +#include +#include +#include + +#define N 500 + +void bubblesort(int* data, int n) { + int k = n-1; + while(k > 0) { + int j = 0; + printf("%d\n",k); + for(int i = 0; i < k; i++) { + if(data[i] > data[i+1]) { + int tmp = data[i]; + data[i] = data[i+1]; + data[i+1] = tmp; + j = i; + } + } + k = j; + } +} + +void shuffle(int* data, unsigned int n) { + for(unsigned int i = 0; i < n - 1; i++) { + unsigned int j = i + rand() % (n - i); + int tmp = data[i]; + data[i] = data[j]; + data[j] = tmp; + } +} + +int main( int argc, char *argv[] ) { + srand((size_t) time(NULL)); + int data[N]; + for(int i = 0; i < N; i++) data[i] = i; + shuffle(data,N); + bubblesort(data, N); + return 0; +}