Pagini recente » Cod sursa (job #1633178) | Cod sursa (job #3290640) | Cod sursa (job #1016279) | Cod sursa (job #296845) | Cod sursa (job #1362244)
#include <stdio.h>
#include <stdlib.h>
#define NMAX 50000
void merge_sort(int v[], int st, int dr) {
if(dr - st < 1) return;
int mid = (st + dr) / 2;
merge_sort(v, st, mid);
merge_sort(v, mid + 1, dr);
int *new_v = malloc(sizeof(int) * (dr - st + 100));
int ind = 0; int a = st; int b = mid + 1;
while(a != mid + 1 || b != dr + 1) {
if(b == dr + 1) { new_v[++ind] = v[a]; ++a; continue; }
if(a == mid + 1) { new_v[++ind] = v[b]; ++b; continue; }
if(v[a] > v[b] ) {
new_v[++ind] = v[b];
++b;
} else {
new_v[++ind] = v[a];
++a;
}
}
for(int i = 1; i <= ind; ++i)
v[st + i - 1] = new_v[i];
}
int main() {
FILE *in = freopen("algsort.in", "r", stdin);
FILE *out = freopen("algsort.out", "w", stdout);
int N; int v[NMAX];
scanf("%d", &N);
for(int i = 1; i <= N; ++i)
scanf("%d", &v[i]);
merge_sort(v, 1, N);
for(int i = 1; i <= N; ++i)
printf("%d ", v[i]);
printf("\n");
fclose(in); fclose(out);
return 0;
}