数组的归并排序:
void mergesort(int a[], int start, int end) {
int mid;
if(start < end) {
mid = (start + end)/2; //找到中间值
mergesort(a, start, mid);
mergesort(a, mid+1, end);
merge(a, start, mid, end); //合并
}
}
void merge(int a[], int start, int mid, int end) {
int n1 = mid - start + 1;
int n2 = end - mid;
int left[n1], right[n2];
int i, j, k;
for(i=0; i<n1; i++) {
left[i] = a[start+i];
}
for(j=0; j<n2; j++) {
right[j] = a[mid+1+j];
}
i=j=k=0;
while(i<n1 && j<n2) {
if(left[i] < right[j]) {
a[k] = left[i];
k++;
i++;
} else {
a[k] = right[j];
k++;
j++;
}
}
while(i<n1) {
a[k] = left[i];
k++;
i++;
}
while(j<n2) {
a[k] = right[j];
k++;
j++;
}
}