반응형
#include <stdio.h>
#define MAXSIZE 100000
const int LM = (int)100001;
int index[LM], temp[LM];
void mergesort(int s, int e, int* a, int* b) {
//1. base condition
if (s >= e)
return;
//2.divide
int mid = (s + e) / 2;
//3. conquer
mergesort(s, mid, a, b);
mergesort(mid + 1, e, a, b);
//4. merge
int i = s, j = mid + 1;
for (int k = s; k <= e; k++) {
if (i > mid)
b[k] = a[j++];
else if (j > e)
b[k] = a[i++];
else if (a[i] > a[j])
b[k] = a[j++];
else
b[k] = a[i++];
}
//5. copy
for (int k = s; k <= e; k++) {
a[k] = b[k];
}
}
반응형