void merge(int array[], int f ,int m, int e){
int *temp = (int *)malloc((e - f +1)*sizeof(int));
int i = f;
int j = m+1;
int k = 0;
int end1 = m;
int end2 = e;
while((i<=end1)&& (j<=end2)){
if(array[i]<=array[j]){
temp[k]=array[i++];
}
else{
temp[k]=array[j++];
}
k++;
}
while(i<=end1){
temp[k++]=array[i++];
}
while(j<=end2){
temp[k++]=array[j++];
}
for (i = 0; i < (e - f +1); i++)
array[f+i] = temp[i];
free(temp);
}
void merge_sort(int array[], unsigned int first, unsigned int last)
{
int mid = 0;
if(first {
mid = (first+last)/2;
merge_sort(array, first, mid);
merge_sort(array, mid+1,last);
merge(array,first,mid,last);
}
}
阅读(933) | 评论(0) | 转发(0) |