#include "stdio.h"
typedef int RecType;
#include "malloc.h"
//将两个有序子文件合并成一个有序文件
void Merge(RecType *R,int low,int m,int high){
RecType *R1;
int i=low;
int j=m+1;
int p=0;
R1=(RecType *)malloc(sizeof(RecType)*(high-low+1));//为新数组申请空间
while(i<=m&&j<=high)//两个子文件都没完
{
R1[p++]=(R[i++]<R[j++])?R[i++]:R[j++];
}
while(i<=m){
R1[p++]=R[i++];
}
while(j<=high){
R1[p++]=R[j++];
}
for(p=0,i=low;i<=high;i++,p++){
R[i]=R1[p];
}
}
void MergeSort(RecType R[],int low,int high){
int mid;
if(low<high){
mid=(high+low)/2;
MergeSort(R,low,mid);
MergeSort(R,mid+1,high);
Merge(R,low,mid,high);
}
}
void main(){
RecType a[]={1,5,2,7,8,4,9};
int low=0,high=6;
int i;
for(i=low;i<=high;i++){
printf("%d ",a[i]);
}
printf("\n");
MergeSort(a,low,high);
for(i=low;i<=high;i++){
printf("%d ",a[i]);
}
}
为什么排序之后a数组的值好像都是地址