#include "stdio.h"
#define MAX 100
int is1[MAX],is2[MAX];
void merge(int low,int mid,int high){
int i=low,j=mid+1,k=low;
while(i<=mid&&j<=high)
if(is1[i]<is1[j])
is2[k++]=is1[i++];
else
is2[k++]=is1[j++];
while(i<=mid)
is2[k++]=is1[i++];
while (j<=high)
is2[k++]=is1[j++];
for ( i = low; i <=high ; i++) {
is1[i]=is2[i];
}
}
void mergeSort(int a, int b){
if(a<b){
int mid=(a+b)/2;
mergeSort(a,mid);
mergeSort(mid+1,b);
merge(a,mid,b);
}
}
int main(){
int i,n;
scanf("%d",&n);
for ( i = 1; i <=n ; ++i) {
scanf("%d",&is1[i]);
}
mergeSort(1,n);
for ( i = 1; i <=n ; ++i) {
printf("%d ",is1[i]);
}
}
算法笔记1.归并排序
最新推荐文章于 2022-01-06 21:24:08 发布