如果我左右创建一个静态数组(在合并函数中),则此方法有效。但是当我动态创建这些数组时(在合并函数的注释中)它不起作用。我找不到问题。请帮忙
using namespace std;
#include <iostream>
int *array;
void mergesort(int array[],int,int);
void merge(int array[],int,int,int);
int main()
{
int n, start, end;
cout<<"Enter the no. of elements: ";
cin>>n;
// int *array;
array = new int(n);
for(int i=0;i<n;i++)
cin>>array[i];
start = 0;
end = n - 1;
mergesort(array,0,n-1);
for(int j=0;j<n;j++)
cout<<array[j];
return 0;
}
void mergesort(int array[],int start,int end)
{
int mid;
if(start < end)
{
mid = (start + end) /2;
mergesort(array,start,mid);
mergesort(array,mid+1,end);
merge(array,start,mid,end);
}
}
void merge(int array[],int start,int mid,int end)
{
int i,j;
int n1 = mid - start + 1;
int n2 = end - mid;
int left[20];
int right[20];
//int *left;
//int *right;
//left = new int(n1);
//right = new int(n2);
for(i = 0;i < n1;i++)
left[i] = array[start + i];
for(j = 0;j < n2;j++)
right[j] = array[mid + j + 1];
left[i] = 32767;
right[j] = 32767;
i = 0;
j = 0;
for(int m = start;m <= end;m++)
{
if(left[i] <= right[j])
{
array[m] = left[i];
i++;
}
else
{
array[m] = right[j];
j++;
}
}
}