Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/151.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 大数组大小的合并排序分段错误_C++_Mergesort - Fatal编程技术网

C++ 大数组大小的合并排序分段错误

C++ 大数组大小的合并排序分段错误,c++,mergesort,C++,Mergesort,我有一个合并排序程序,它可以很好地处理较小的数组大小,但是当我尝试对大小为1000的数组进行排序时,会出现分段错误。尝试调试程序后,seg故障似乎发生在(i

我有一个合并排序程序,它可以很好地处理较小的数组大小,但是当我尝试对大小为1000的数组进行排序时,会出现分段错误。尝试调试程序后,seg故障似乎发生在(i时的代码块
周围的某个地方,但我很难找出哪里出了问题

void mergeSort(long array[], long last1, long last2){
  //Elements in first, second, and merged arrays, respectively                                                                       
  long i = 0;
  long j = last1;
  long k = 0;

  long* temp = new long[last2];

  //Copy the smaller of the two values i and j into the new temp array                                                               
  while(i < last1 && j < last2){
    if(array[i] < array[j]){
      temp[k] = array[i];
      ++i;
      ++k;
    }
    else{
      temp[k] = array[j];
      ++j;
      ++k;
    }
  }

  //Copy the remaining largest elements of the two arrays to temp[]                                                                  
  while(i < last1){
    temp[k] = array[i];
    ++i;
    ++k;
  }
  while(j < last2){
    temp[k] = array[j];
    ++j;
    ++k;
  }

  //Copy the merged elements of the temp array back into the original array                                                          
  for(long m=0; m<last2; ++m){
    array[m] = temp[m];
  }
}


int main(){

 //create array here called array and fill will values

  for(long i=1; i<n; i=(2*i)){
    for(long j=0; j<(n-1); j=(j+(2*i))){
      long end = ((2*i) < (n-j)) ? (2*i) : (n - j);
        function.mergeSort(&array[j], i, end);
    }
  }
  return 0;
}
void mergeSort(长数组[],长last1,长last2){
//第一、第二和合并数组中的元素
长i=0;
长j=last1;
长k=0;
long*temp=新的long[last2];
//将两个值i和j中较小的值复制到新的临时数组中
而(i对于(long m=0;mYou确实意识到,当您将两个数组合并到
temp
中时,总长度将是两个输入的总和,对吗?此外,我看到您
new[]temp
,但没有
delete[]temp
。这看起来不像是正常的合并排序调用。这不是一个合并排序算法,而是一个合并算法。您缺少了合并排序的整个前端,即正确的分区机制。