c实现归并排序

#include
int a[9] = { 0,8,4,5,7,1,3,6,2 };
int k[9];
void merge(int l, int r, int mid){
	
	int i = l, j = mid,t=l;
	while (i < mid&&j <=r){
		if (a[i] <= a[j]) k[t++] = a[i++];
		else k[t++] = a[j++];
	}
	while (i < mid) k[t++] = a[i++];
	while (j <= r) k[t++] = a[j++];
	int p = l;
	i = l;
	while (p <= r){
		a[i++] = k[p++];
	}
	
}
void fout(int l, int r){
	if (l < r){
		int mid = (l + r) / 2;
		fout(l, mid);//右
		fout(mid+1 , r);//左
		merge(l, r, mid+1);
	}
}
int main(){
	fout(1, 8);
	for (int i = 0; i <9; i++)
		printf("%d   ",k[i]);
	return 0;
} 

你可能感兴趣的:(算法,数据结构,排序算法)