简体   繁体   English

如何使C ++递归程序终止

[英]How to make the C++ recursion program terminate

Why the program does not terminate after the return and how to make it terminate ? 为什么程序在返回后没有终止,以及如何使其终止? https://ideone.com/9Lz6jy Note: The intent here is to find median if that helps in understanding the program. https://ideone.com/9Lz6jy注意:此处的目的是找到中位数,以帮助理解程序。 But my question is pure C++ related. 但是我的问题是纯C ++相关的。 No help needed in finding median. 查找中位数无需帮助。 Please focus on how I can return the answer once I have fount it. 一旦找到答案,请专注于如何返回答案。

#include <iostream>
#include <time.h>
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <assert.h> 

using namespace std;


int Pivot2(vector<int> &v, int pivot) {

    vector<int> v_copy(v.size()); 
    //int pivot = v.size() / 2; 
    //1. Sort the array about the mid term  
    int count_less = 0; 
    int j = 0; 
    for (unsigned int i = 0; i <v.size() ; i++) {

        if (v[i]< v[pivot]) {
            v_copy[j]=v[i]; 
            j++; 
            count_less++; 
        }
    }

    v_copy[j]=v[pivot];
    j++; 

    for (unsigned int i = 0; i <v.size(); i++) {

        if (v[i]> v[pivot]) {
            v_copy[j] = v[i];
            j++; 
        }
    }

    //2.  if the number of less than  than tmp_med increase the middle postion 
    if ( count_less >  v.size() / 2) {
        Pivot2(v_copy,count_less-1);
    }
    else if (count_less ==  v.size() / 2) {
        cout <<"inner " <<  v[pivot] <<endl ; 
        return v[pivot]; //Why the recursion does not terminate with this return?
    }
    else {
        if ( count_less < v.size() / 2) {
            Pivot2(v_copy, count_less + 1 );
        }
    }



}


int main() {
    // your code goes here
    int arr[] = { 8, 7, 3, 1, 9, 4, 6, 5, 2};
    int n = sizeof(arr) / sizeof(arr[0]); 
    //randomize(arr, n);
    vector<int> v( begin(arr), end(arr) );

    int med1 = Pivot2(v,v.size()/2);
    assert(5 == med1);
    cout << med1 <<endl ; 
    return 0;
}

First of all, enable compilers warnings as you are doing several comparisons between signed and unsigned integer expressions. 首先,在对有符号和无符号整数表达式进行几次比较时,启用编译器警告。 ie : if ( count_less > v.size() / 2) { 例如: if ( count_less > v.size() / 2) {

Then you need to return the Pivots you create 然后,您需要返回创建的数据Pivots

    return Pivot2(v_copy,count_less - 1);
    ^^^^^^^^
    .....
    return Pivot2(v_copy, count_less + 1 );
    ^^^^^^

Also, be careful that your function reaches end of non-void function . 另外,请注意您的函数会reaches end of non-void function Simply remove if ( count_less < v.size() / 2) { and it will be fine. 只需删除if ( count_less < v.size() / 2) {这样就可以了。 ie: 即:

else {
    return Pivot2(v_copy, count_less + 1 );
}

You can check this version on Coliru 您可以在Coliru上检查此版本

You need to return values in all condition from this block: 您需要从此块返回所有条件的值:

if ( count_less >  v.size() / 2) {
    return Pivot2(v_copy,count_less-1);
}
else if (count_less ==  v.size() / 2) {
    cout <<"inner " <<  v[pivot] <<endl ; 
    return v[pivot]; //Why the recursion does not terminate with this return?
}
else {
    if ( count_less < v.size() / 2) {
        return Pivot2(v_copy, count_less + 1 );
    }
}

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

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