简体   繁体   English

在Int C ++数组上合并排序

[英]Merge Sort on Array of Int C++

I'm trying to create a program merge-sort on an array of int butI keep having troubles running this merge sort, it gives me a segment fault but I couldn't find anything wrong with it. 我正在尝试在一个int数组上创建一个程序merge-sort,但是我在运行此merge sort时遇到了麻烦,这给我带来了段错误,但我找不到任何错误。 In void mergesort when I put first <= last then the segment fault appears if not, then 5 5 5 5 is being print. 在void mergesort中,当我先放置<=时,如果不是,则出现段错误,则正在打印5 5 5 5。

#include <iostream>

 using namespace std;


void merge(int *arr, int size, int first, int middle, int last)
{
    int temp[size];
    for(int i = first; i<=last; i++)
    {
       temp[i] = arr[i];
    }
    int i=first, j=middle+1, k=0;
    while(i<=middle && j<=last)
    {
       if(temp[i] <= temp[j])
       {
          arr[k] = temp[i];
          i++;
       }
       else
       {
          arr[k]=temp[i];
          j++;
       }
       k++;
    }
    while(i<=middle)
    {
       arr[k]=temp[i];
       k++;
       i++;
    }
}

void mergesort(int *arr, int size, int first, int last)
{
    if(first<last)
    {
       int middle = ( first + last )/2;
       mergesort(arr,size,first,middle);
       mergesort(arr,size,middle+1,last);
       merge(arr,size,first,middle,last);
    }
}
int main()
{
    cout <<"Him";
    const int size = 10;
    int numbers [] = {5,10,1,6,2,9,3,8,7,4};
    mergesort(numbers,size,0,9);
    for( int i= 0; i<size; ++i)
    {
        cout << numbers[i] << " ";
    }
    return 0;
}

There are (at least) two bugs. 有(至少)两个错误。 This: 这个:

else
{
   arr[k]=temp[i];                                          
   j++;
}

should be this: 应该是这样的:

else
{
   arr[k]=temp[j];                                          
   j++;
}

and this: 和这个:

int i=first, j=middle+1, k=0;

should be this: 应该是这样的:

int i=first, j=middle+1, k=first;

In general, you ought to learn to step through the code, at least by putting diagnostic output statements here and there. 通常,至少应该通过在此处四处放置诊断输出语句来学习逐步执行代码。 Once you have the hang of that you can move up to a good debugger. 一旦掌握了这些,就可以升级到一个好的调试器。

The standard library already implements a function that merges correctly: std::inplace_merge . 标准库已经实现了可以正确合并的功能: std::inplace_merge Implementation adapted from this more general post 改编自此更一般的帖子

void mergesort(int * first, int * last)
{
    std::ptrdiff_t N = std::distance(first, last);
    if (N <= 1) return;                   
    int * middle = std::next(first, N / 2);
    mergesort(first, middle); 
    mergesort(middle, last);  
    std::inplace_merge(first, middle, last); 
}

int main()
{
    cout <<"Him";
    const int size = 10;
    int numbers [] = {5,10,1,6,2,9,3,8,7,4};
    mergesort(numbers, numbers+size);
    for( int i= 0; i<size; ++i)
    {
        cout << numbers[i] << " ";
    }
    return 0;
}

Suggestion 1: 建议1:

Instead of that line: 代替该行:

int temp[size];

If you need a dynamic size array use: 如果需要动态大小数组,请使用:

int temp = new int[size];

Then once you are done with it 然后,一旦你完成它

delete[] temp;

Edit: As Neil suggested using std::vector is may be more useful than arrays in such situations (if you are allowed to use it). 编辑:正如尼尔建议的那样,在这种情况下使用std :: vector可能比数组有用(如果允许使用它)。

Your code has 3 bugs , Also you can reduce your code length too if required. 您的代码有3个错误 ,如果需要,您也可以减少代码长度。

void merge(int *arr, int size, int first, int middle, int last)
{
    int temp[size];
    for(int i = first; i<=last; i++)
      temp[i] = arr[i];
    int i=first, j=middle+1, k=first; // 1st Change, Set k to first instead of 0
    while(i<=middle && j<=last)
    {
       if(temp[i] <= temp[j])
          arr[k++] = temp[i++];
       else
          arr[k++]=temp[j++]; // 2nd Change, use j instead of i
    }
    while(i<=middle)
       arr[k++]=temp[i++];
    while(j<=last)    // 3rd Change you missed this case
        arr[k++]=temp[j++];
}

Live Code 现场代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM