【问题标题】:Random ouput with C++ Recursive Merge Sort使用 C++ 递归合并排序的随机输出
【发布时间】:2012-08-18 14:58:35
【问题描述】:

我按照这个递归的algorithm 进行了维基百科上详细介绍的合并排序。

这是我想出的代码:

int* merge(int left[], int leftSize, int right[], int rightSize){
int result[leftSize + rightSize];   //The merged array
int resultPointer = 0;  //Index position of where to insert element
int leftPointer = 0;
int rightPointer = 0;

//While length of either of the lists is > 0
while((leftSize > 0) || (rightSize > 0)){

    cout << "Got here" << endl;
    //If length of both left and right lists is > 0
    if((leftSize > 0) && (rightSize > 0)){

        //Compare first elements from both lists and put smallest one in the result list
        if(left[0] < right[0]){
            result[resultPointer] = left[0];
            leftPointer++;
            leftSize--;
        }else if(right[0] < left[0]){
            result[resultPointer] = right[0];
            rightPointer++;
            rightSize--;
        }else{
            //if both elements are the same, put them both in the result list
            result[resultPointer] = left[0];
            leftPointer++;
            leftSize--;
            result[resultPointer++] = right[0];
            rightPointer++;
            rightSize--;
        }
        resultPointer++;    //Increment pointer to point to next empty element

    }else if(leftSize > 0){
        result[resultPointer] = left[0];
        leftPointer++;
        leftSize--;
    }else if(rightSize > 0){
        result[resultPointer] = right[0];
        rightPointer++;
        rightSize--;
    }


}

//int* resultList = result;

return result;
}

int* merge_sort(int list[], int size){

//If list has 1 element then it is sorted so just return that
if(size<=1){
    return list;
}

int middle = size/2;    //Get mid point of given list

//Create left and right arrays
int left[middle];
int right[size-middle];

for(int i = 0; i<size-middle; i++){

    if(i<middle){
        left[i] = list[i];
    }
    right[i] = list[i+middle];
}

//Recursively call merge sort to sort out the sublists
int* leftList = merge_sort(left, middle);
int* rightList = merge_sort(right, size-middle);

//Merge the sorted lists and return a fully sorted list

int* merged = merge(leftList, middle, rightList, size-middle);

return merged;
}

生成的输出只是一堆随机数(我认为当某些东西尚未初始化时会发生这种情况)。这个实现可能很愚蠢,但我仍然想知道这里出了什么问题。

干杯

【问题讨论】:

    标签: c++ sorting recursion mergesort


    【解决方案1】:

    您正在从合并函数返回一个指向局部变量的指针。当您从合并函数返回时,局部变量将超出范围。所以你返回一个没有指向任何有效内存的指针。

    【讨论】:

    • 啊。那么我该如何纠正呢?通过将 result 设为全局变量?
    • 全局变量总是一个坏主意。在这种情况下,我认为最好将第三个缓冲区传递给合并,该缓冲区足够大以容纳结果。
    • 感谢您的回答。但是,我最后只是使用了向量,如下所示:en.wikibooks.org/wiki/Algorithm_Implementation/Sorting/…
    【解决方案2】:

    您的合并函数正在返回一个指向局部变量的指针。

    【讨论】:

      【解决方案3】:

      您遇到的不仅仅是简单的初始化问题:
      编译时出现以下错误:

      > g++ -Wall -Wextra -pedantic merge.cpp  
      
      merge.cpp: In function ‘int* merge(int*, int, int*, int)’:   
      merge.cpp: 2: error: ISO C++ forbids variable-size array ‘result’  
      merge.cpp:10: error: ‘cout’ was not declared in this scope  
      merge.cpp:10: error: ‘endl’ was not declared in this scope  
      merge.cpp: 2: warning: address of local variable ‘result’ returned  
      
      merge.cpp: In function ‘int* merge_sort(int*, int)’:  
      merge.cpp:62: error: ISO C++ forbids variable-size array ‘left’  
      merge.cpp:63: error: ISO C++ forbids variable-size array ‘right’  
      

      我会说:

      merge.cpp: 2: warning: address of local variable ‘result’ returned  
      

      这很关键。但是你应该把它们都修好。

      【讨论】:

      • 这很奇怪,因为我没有收到任何其他错误。
      猜你喜欢
      • 2015-04-22
      • 2021-08-18
      • 2014-01-21
      • 2017-12-22
      • 1970-01-01
      • 2021-11-06
      • 2019-01-22
      相关资源
      最近更新 更多