简体   繁体   English

从递归函数传递指针

[英]Passing pointer from recursive function

I've been ask to make selection sort code with only recursive method. 我一直被要求仅使用递归方法来进行选择排序代码。 So i think about making another function to find array that store max value then switch it in my other function. 因此,我考虑使用另一个函数来查找存储最大值的数组,然后将其切换到另一个函数中。

void p_rec_max(int data[], int cur, int arrSize,int * x) {
    if(cur < arrSize - 1) {
        if(data[cur] > data[arrSize - 1]) {
            *x = cur;
        } else if(data[cur] < data[arrSize - 1]){
            *x = arrSize - 1;
        }
        p_rec_max(data,cur + 1,arrSize,&x);
    }
}

void rec_selection_sort(int data[], int arrSize) {
    if(arrSize > 0) {
        int maxi,temp;
        p_rec_max(data,0,arrSize,&maxi);
        temp = data[arrSize - 1];
        data[arrSize - 1] = data[maxi];
        data[maxi] = temp;
        rec_selection_sort(data,arrSize - 1);
    }
}

It got some warning like this 收到这样的警告

In function 'p_rec_max': warning: passing argument 4 of 'p_rec_max' from incompatible pointer type [enabled by default] note: expected 'int *' but argument is of type 'int **' 在函数'p_rec_max'中:警告:从不兼容的指针类型传递'p_rec_max'的参数4 [默认启用]注意:预期为'int *',但参数的类型为'int **'

And it does not change my array at all. 而且它根本不会改变我的数组。 My lack of information of passing pointer in function can't help me to fix that problem. 我缺少在函数中传递指针的信息,无法帮助我解决该问题。 Could you guys fix my code and then explain to me of what is wrong about my code? 你们可以修复我的代码,然后向我解释我的代码有什么问题吗? Thanks 谢谢

First problem 第一个问题

You have a problem with this line: 您对此行有疑问:

p_rec_max(data,cur + 1,arrSize,&x);
                               ^^

Since x is a int * , &x is a int ** which isn't what the function expects. 由于xint *&xint ** ,这不是函数期望的。

Change the line to: 将行更改为:

p_rec_max(data,cur + 1,arrSize, x);

ie no & in front of x 即否& x前面

Second problem 第二个问题

Your p_rec_max function doesn't find the index of the maximum array value. 您的p_rec_max函数找不到最大数组值的索引。

If all array elements are the same, the code will never execute a *x = ... . 如果所有数组元素都相同,则代码将永远不会执行*x = ... In other words maxi will never be written and you end up using an uninitialized index here data[arrSize - 1] = data[maxi]; 换句话说, maxi将永远不会被写入,并且您最终将使用未初始化的索引data[arrSize - 1] = data[maxi]; That is undefined behavior and may crash your program. 这是未定义的行为,可能会使您的程序崩溃。

Besides that I think the basic logic in the function is wrong. 除此之外,我认为函数中的基本逻辑是错误的。 The code always compare cur to the last array element. 代码始终将cur与最后一个数组元素进行比较。 That seems wrong. 好像错了 I think you should compare cur to the value of the maximum found so far. 我认为您应该将cur与迄今为止找到的最大值进行比较。 That could be done using the variable x . 可以使用变量x来完成。

Something like: 就像是:

void p_rec_max(int data[], int cur, int arrSize,int * x) {
    if(cur < arrSize) {
        if(data[cur] > data[*x]) {
            *x = cur;
        }
        p_rec_max(data, cur + 1, arrSize, x);
    }
}

and in rec_selection_sort call it like: 并在rec_selection_sort像这样调用它:

    maxi = 0;  // Assume index zero holds the maximum
    p_rec_max(data, 1, arrSize, &maxi);
                    ^
                 Start searching from index 1

BTW: Using a recursive function to find the maximum value in an array is of cause not a good approach but I guess you are not allowed to use a simple for or while loop. 顺便说一句:使用递归函数在数组中查找最大值不是一个好的方法,但是我想您不能使用简单的forwhile循环。

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

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