简体   繁体   English

C ++递归地找到数组的最小值

[英]C++ recursively find minimum value of array

I have an assignment for a c++ programming class to write a recursive function without the use of static variables, with the following prototype: int findmin(const int a[], int n); 我有一个c ++编程类的作业,用于在不使用静态变量的情况下编写递归函数,使用以下原型:int findmin(const int a [],int n);

My solution works (for very small arrays), however I think ~2^n complexity is excessive and could be improved. 我的解决方案是有效的(对于非常小的数组),但我认为~2 ^ n复杂度过高并且可以改进。

Are there any improvements that could be made within the specified criteria that would make this more efficient? 是否可以在指定的标准内进行任何改进,以提高效率?

int findmin(const int a[], int n)
{
    if(n == 0)
        return a[0];
    else
    {
        if(a[n-1] < findmin(a,(n-1)))
            return a[n-1];
      else
            return findmin(a,(n-1));
    }
}

It's a little silly to worry about efficiency, given that there is an obvious, non-recursive way to do it in O(n), one pass. 考虑到在O(n)中有一种明显的,非递归的方式,一次通过,担心效率有点愚蠢。 There is even an STL algorithm std::min_element. 甚至有一个STL算法std :: min_element。 But then, it's a silly assignment. 但是,这是一个愚蠢的任务。 FIrst be sure your solution is correct. 首先要确保您的解决方案是正确的。 When n==0, will a[0] be valid? 当n == 0时,a [0]是否有效? Generally, such an n indicates the length of the array, not the lowest index. 通常,这样的n表示阵列的长度,而不是最低的索引。

To go from O[n^2] to O[n], be sure to compare each element only once. 要从O [n ^ 2]转到O [n],请务必仅比较每个元素一次。 That implies not starting at the beginning of the array on every pass. 这意味着不会在每次传递时从数组的开头开始。

#include <algorithm>
#include <cassert>

int findmin(const int a[], int n)
{
    assert(n>0);
    if(n == 1)  // See heyah.
        return a[0];
    else
    {
        return std::min(a[0], findmin(a + 1, n - 1));
    }
}

In for-real C++ code, if for some reason we were saddled with the old fashion function signature, we would do something like this: 在for-real C ++代码中,如果由于某种原因我们背负旧时尚功能签名,我们会做这样的事情:

int findmin(const int a[], int n) {
    if(n<=0) { throw std::length_error("findmin called on empty array");}
    return *std::min_element(a, a+n);
}

You could do conditional operator ?: to get rid of bunch if else statements, to make function cleaner. 你可以做条件运算符?:摆脱束if else语句,使函数更清晰。 And instead of calling findmin() twice you could assign return value to variable inside of the statement, this is main advantage of this code vs. original one. 而不是两次调用findmin() ,你可以将返回值赋给语句内部的变量,这是此代码与原始代码的主要优点。

int findmin(const int a[], int n) {
   if (n == 0) // base case
      return a[0];

   return a[n] < (int min = findmin(a, n - 1)) ? a[n] : min;
}

This ( a[n] < (int min = findmin(a, n - 1)) ? a[n] : min; ) could be done using if statement as well: 这个( a[n] < (int min = findmin(a, n - 1)) ? a[n] : min; )也可以使用if语句完成:

if (a[n] < (int min = findmin (a, n - 1))
     return a[n];
else
     return min;

EDIT: Per many reputable sources, this is O(n) time. 编辑:根据许多信誉良好的来源,这是O(n)时间。 O (n^2) would be if we are comparing each element with all the other elements. O(n ^ 2)将是我们将每个元素与所有其他元素进行比较。

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

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