one-file-projects/bubblesort.c

41 lines
851 B
C
Raw Permalink Normal View History

2016-02-22 19:24:15 +01:00
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#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;
}