算法小白的学习之路:day02-归并排序——递归版
#include<stdio.h>
void merge(int a[],int a_left,int a_right,int b_left,int b_right){
int i=a_left,j=b_left;
int count=0;
int temp[100];
while(i<=a_right && j<=b_right){
if(a[i]<a[j]){
temp[count++]=a[i++];
}
else{
temp[count++]=a[j++];
}
}
while(i<=a_right){
temp[count++]=a[i++];
}
while(j<=b_right){
temp[count++]=a[j++];
}
for(int k=0;k<count;k++){
a[a_left+k]=temp[k];
}
}
void mergesort(int a[],int left,int right){
int mid=(left+right)/2;
if(left<right){
mergesort(a,left,mid);
mergesort(a,mid+1,right);
merge(a,left,mid,mid+1,right);
}
}
int main(void){
int a[10]={1,3,5,10,9,2,4,6,8,7};
mergesort(a,0,9);
for(int i=0;i<10;i++){
printf("%d ",a[i]);
}
}