简体   繁体   English

数组中的多路径递归最大值

[英]Multipath recursion Maximum Value in Array

I am having trouble writing a multipath recursive function given an unsorted integer array.给定一个未排序的整数数组,我在编写多路径递归函数时遇到问题。

std::max(maxArray(a[],first,last))

I understand that I need to split the arrays in half and use the std::max function that compares two integers such as:我知道我需要将数组分成两半并使用std::max函数来比较两个整数,例如:

return std::max(maxArray(a[],Not sure, Not Sure),maxArray(a[], Not sure, Not sure))

I am familiar with the Binary Search Algorithm.我熟悉二分搜索算法。 However, I am struggling to see how I can solve this without just comparing the two numbers from the left and right side of the array.但是,我正在努力寻找如何在不比较数组左侧和右侧的两个数字的情况下解决这个问题。 Won't those two numbers just return the greater of the 2 compared and then get trashed?这两个数字不会只返回比较的 2 中的较大者然后被丢弃吗?

I have seen the other posts about this same problem however it does not adhere to the pseudocode that was given.我看过其他关于这个问题的帖子,但是它不符合给出的伪代码。 Any help would be greatly appreciated.任何帮助将不胜感激。

I'm struggling to understand your explaination of what it is you don't understand.我正在努力理解您对您不理解的内容的解释。 But the principle behind the code you've been given is that if you divide an array into two halves and find (recursively) the maximum value in each half, then the maximum value for the whole array is the larger of those two values.但是你得到的代码背后的原理是,如果你将一个数组分成两半并(递归地)找到每一半的最大值,那么整个数组的最大值是这两个值中较大的一个。

Something like this像这样的东西

int maxArray(const int* a, int left, int right)
{
    if (left == right)
    {
        return a[left];
    }
    else
    {
        int mid = (left + right)/2;
        return std::max(
            maxArray(a, left, mid),
            maxArray(a, mid + 1, right));
    }
}

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

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