简体   繁体   English

合并排序分段错误c ++

[英]merge sort segmentation fault c++

I am trying to implement merge sort for my algorithm analysis class and every time I run it there is a segmentation fault. 我正在尝试为我的算法分析类实施合并排序,并且每次运行它时,都会出现分段错误。 I think the problem is when i split the vector in the merge_sort function but I cannot find the problem. 我认为问题是当我在merge_sort函数中分割向量时,却找不到问题。 Help would be really appreciated guys. 帮助将是非常感激的家伙。

template <typename T>
std::vector<int> merge(std::vector<T>& A,std::vector<T>& B)
{
   int a_size = A.size();
   int b_size = B.size();
   std::vector<int> C(a_size+b_size,0);
   //int *c = new int[b_size+a_size];
   int i =0,j =0,k=0;
   while(i < a_size && j < b_size)
   {
        if(A[i]<B[j])
        {
            C[k] = A[i];
            k++;
            i++;
        }   
        else
        {
            C[k] = B[j];
            k++;
            j++;
            if(i!=a_size)
            {
                for(;i<a_size;i++,k++)
                {
                    //copy rest of a to c
                    C[k] = A[i];
                }
            }
            if(j != b_size)
            {
                for(;j<b_size;k++,j++)
                {
                    //copy the rest of b to c
                    C[k] = B[j];
                }
            }   
        }
    }
    return C;
}
// Merge sort implementation
template <typename T>
void merge_sort(std::vector<T>& vector)
{
    // TODO implement merge sort
    int vector_size = vector.size();
    int big_vector_index = 0;
    int half_size = (int)vector_size/2;
    int remainder = vector_size%2;
    std::vector<int> left(half_size,0);
    std::vector<int> right(half_size+remainder,0);
    for(int l = 0;big_vector_index<half_size;l++,big_vector_index++)
    {
        left[l] = vector[big_vector_index];
    }

    for(int m = 0;big_vector_index<vector_size;m++,big_vector_index++)
    {
        right[m] = vector[big_vector_index];
    }
    big_vector_index = 0;
    merge_sort(left);
    merge_sort(right);
    vector = merge(left,right);

}

I took a look at your code, and the majority of it is correct from my testing. 我看了看您的代码,大部分代码在我的测试中都是正确的。 I don't want to do your coursework for you but maybe a couple of hints in the right direction will help. 我不想为您做课程,但是也许有一些正确方向的提示会有所帮助。

For your original question about the segfault that you got, PaulMcKenzie, Jim Lewis, and Tahlil are right, merge_sort() needs a base condition (base case) to check whether or not the recursion should continue, so it doesn't run forever and/or until your computer runs out of memory (which is what is happening in your case). 对于您关于段错误的原始问题,PaulMcKenzie,Jim Lewis和merge_sort()是正确的, merge_sort()需要一个基本条件(基本情况)来检查是否应继续递归,因此递归不会永远持续下去,并且/或者直到您的计算机内存不足为止(这就是您的情况)。 In general, any recursive function should have a base case. 通常,任何递归函数都应具有基本情况。

Also, you should take a look at your merge function as well. 另外,您还应该查看合并功能。 It has all the parts of a correct merge function, but some parts of it run a bit earlier/more often than you want them too. 它具有正确合并功能的所有部分,但其中某些部分的运行时间比您希望的要早/经常。 I don't want to give too much away since it's for class, but if you fix the segfault problem and are still getting strange answers, take a look at it. 由于要上课,我不想付出太多,但是如果您解决了段错误并仍然得到奇怪的答案,请查看一下。

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

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