作者在 2015-11-06 23:51:00 发布以下内容
插入排序代码:
/*插入排序*/
void insertionsort(int *arr,int n){
int i,j;
for(j = 1;j < n;j++){
int k = arr[j];
i = j-1;
while(i >-1 && arr[i] < k){
arr[i+1] = arr[i];
i--;
}
arr[i+1] = k;
}
}
/*
*归并排序
*/
void merge_sort(int *arr,int fi,int la){
if(fi < la){
merge_sort(arr,fi,(fi+la)/2);
merge_sort(arr,(fi+la)/2 + 1,la);
merge(arr,fi,(fi+la)/2,la);
}
}
void merge(int *arr,int fi,int mi,int la){
int i,m=0,n=0;
int *left = (int *)malloc(sizeof(int)*(mi-fi+2));
int *right = (int *)malloc(sizeof(int)*(la-mi+1));
left[mi-fi+1] = 100;
right[la-mi] = 100;
for(i=0;i<=mi-fi;i++)
left[i] = arr[fi+i];
for(i=0;i<la-mi;i++)
right[i] = arr[mi+i+1];
for(i=fi;i<la+1;i++){
if(left[m] < right[n]){
arr[i] = left[m];
m++;
}else{
arr[i] = right[n];
n++;
}
}
}