访问违例(段异常)求指点
#include <stdio.h>#include <stdlib.h>
void SortIntegerArray(int array[], int n);
static void Merge(int array[], int arr1[], int n1, int arr2[], int n2);
void SortIntegerArray(int array[], int n)
{
int i, n1, n2;
int *arr1, *arr2;
if(n > 1){
n1 = n / 2;
n2 = n - n1;
arr1 = (int *)malloc(n1*sizeof(int));
arr2 = (int *)malloc(n2*sizeof(int));
for(i=0;i<n1;i++) arr1[i]=array[i];
for(i=0;i<n2;i++) arr2[i]=array[n1+i];
SortIntegerArray(arr1,n1);
SortIntegerArray(arr2,n2);
Merge(array, arr1, n1, arr2, n2);
}
}
static void Merge(int array[], int arr1[], int n1, int arr2[], int n2)
{
int p, p1, p2;
p=p1=p2=0;
while(p1<n1&&p2<n2){
if(arr1[p1]<arr2[p2]) array[p++]=arr1[p1++];
else array[p++]=arr2[p2++];
}
while(p1<n1) array[p++]=arr1[p1++];
while(p2<n2) array[p++]=arr2[p2++];
}
int main()
{
int n, i, *array;
printf("Please input how many numbers you want to sort:\n");
scanf("%d", &n);
printf("Please input the numbers:\n");
array=(int *)malloc(n*sizeof(int));
for(i=0;i<n;i++) scanf("%d", array[i]);
SortIntegerArray(array, n);
for(i=0;i<n;i++) printf("%d ", array[i]);
system("PAUSE");
return 0;
}