EPA: bubblesort
This commit is contained in:
parent
0ba778e0d2
commit
c14af4fe93
1 changed files with 40 additions and 0 deletions
40
bubblesort.c
Normal file
40
bubblesort.c
Normal file
|
@ -0,0 +1,40 @@
|
|||
#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;
|
||||
}
|
Loading…
Reference in a new issue