求助 c语言写的归并排序
运行输入10个整数后,tc自动关闭,不知为何???在申请空间处理的error函数要用什么文件头?
/*mergesort 2009-8-15 write by qiuli*/
#include <stdio.h>
#include <alloc.h>
#include <conio.h>
void mergesort(int num[],int n);
void mergepass(int num[],int length,int n);
void merge(int num[],int low,int m,int high);
main()
{int num[11],i;
clrscr();
printf("Insert ten number to sorting by inserting-sort !\n");
for(i=1;i<11;i++)
scanf("%d",&num[i]);
mergesort(num,10);
for(i=1;i<11;i++)
printf("%d ",num[i]);
getch();
return 0;
}
void mergesort(int num[],int n)
{int length=1;
for(length=1;length<n;length*=2)
mergepass(num,length,n);
}
void mergepass(int num[],int length,int n)
{ int i;
for(i=1;i+2*length-1<=n;length*=2)
merge(num,i,i+length-1,i+2*length-1);
if(i+length-1<n)
merge(num,i,i+length-1,n);
}
void merge(int num[],int low,int m,int high)
{ int i=low,j=m+1,p=0;
int *r=(int*)malloc((high-low+1)*sizeof(int));
/*if(!r)
Error("no enough space to obtain!");*/
while(i<=m&&j<=high)
r[p++]=(num[i]<=num[j])?num[i]:num[j];
while(i<=m)
r[p++]=num[i++];
while(j<=high)
r[p++]=num[j++];
for(p=0,i=low;i<=high;p++,i++)
num[i]=r[p];
}