简体   繁体   English

通过递归在数组中查找最大值

[英]Find maximum value in an array by recursion

// Find a maximum element in the array.
findMax(A)
   findMaxHelper(A, 0, A.length)

findMaxHelper(A, left, right)
   if (left == right - 1) 
      return A[left]
   else
      max1 = findMaxHelper(A, left, (right + left) / 2)
      max2 = findMaxHelper(A, (right + left) / 2, right)

      if (max1 > max2) 
         return max1 
      else 
         return max2

I am having a hard time understanding what is happening in this pseudo-code. 我很难理解这个伪代码中发生了什么。

Can someone help explain what is happening at each line. 有人可以帮助解释每一行发生的事情。 I need to understand this code before I can answer the questions. 在我回答问题之前,我需要先理解这段代码。

I know that the function findMax calls the helper function findMaxHelper, then findMaxHelper uses recursion. 我知道函数findMax调用辅助函数findMaxHelper,然后findMaxHelper使用递归。 Other than that, I really don't understand it. 除此之外,我真的不明白。

You are using Divide and Conquer algorithm for finding the maximum element from the array. 您正在使用Divide and Conquer算法来查找数组中的最大元素。 First you are dividing the array into individual elements(divide), then you are comparing the elements(conquer). 首先,您将数组划分为单个元素(除法),然后比较元素(征服)。 You are dividing the array using calling findMaxHelper recursively. 您正在使用递归调用findMaxHelper来划分数组。

The general idea of Divide and conquer is shown in the figure: 分而治之的总体思路如下图所示:

在此输入图像描述

Example: 例:

在此输入图像描述 Here max is same as your findMaxHelper function with two arguments ie left and right . 这里maxfindMaxHelper函数相同,有两个参数,即leftright

Check this example for more in depth understanding of the concept. 查看示例以更深入地了解该概念。

Jaguar has put the concept quite nicely and Paul has provided correct and detailed explanation. 捷豹已经很好地理解了这个概念,保罗提供了正确而详细的解释。 To add to this , I would like to share a simple C code that gives you an idea how the code gets executed. 除此之外,我想分享一个简单的C代码,让您了解代码是如何执行的。 Here's the code with the same input Jaguar used : 这是使用Jaguar的相同输入的代码:

#include<stdio.h>
int findMaxHelper(int A[], int left, int right){
   int max1,max2;
   int static tabcount;
   int loop;
   for(loop = 0 ; loop <tabcount;loop++) printf("\t");
   tabcount++;
   printf(" Entering: findMaxHelper(A, left = %d ,right = %d)\n\n",left,right);
   if (left == right - 1){ 
      for(loop = 0 ; loop <tabcount;loop++) printf("\t");
      printf("\b\b\b\b\b\b\bLeaving: findMaxHelper(A, left = %d ,right = %d)| returning %d\n\n",left,right , A[left]);
      tabcount--;
      return A[left];
   }
   else
   {
      max1 = findMaxHelper(A, left, (right + left) / 2);
      max2 = findMaxHelper(A, (right + left) / 2, right);

      if (max1 > max2){ 
    for(loop = 0 ; loop <tabcount;loop++) printf("\t");
    printf("\b\b\b\b\b\b\bLeaving: findMaxHelper(A, left = %d ,right = %d) | returning max1=%d\n\n",left,right,max1);
    tabcount--;
    return max1;
    }
      else {
     for(loop = 0 ; loop <tabcount;loop++) printf("\t");
     printf("\b\b\b\b\b\b\bLeaving: findMaxHelper(A, left = %d ,right = %d)| returning max2=%d\n\n",left,right,max2);
     tabcount--;
     return max2;
    }

   }
}

int main (){
    int A[] = { 34,3,47,91,32,0 };
    int Ans =findMaxHelper(A,0,7);  
    printf( "And The Answer Is = %d \n",Ans);
}

U can copy paste the code on ur linux machine ...Maybe put sleep(5) after every printf and see how recursion ACTUALLY works !... Hope this helps... I will also share the output from my system here : 你可以复制粘贴你的linux机器上的代码...也许在每个printf之后放入sleep(5)并看看递归是如何工作的!...希望这有帮助......我也将在这里分享我系统的输出:

Entering: findMaxHelper(A, left = 0 ,right = 7)

     Entering: findMaxHelper(A, left = 0 ,right = 3)

         Entering: findMaxHelper(A, left = 0 ,right = 1)

         Leaving: findMaxHelper(A, left = 0 ,right = 1)| returning 34

         Entering: findMaxHelper(A, left = 1 ,right = 3)

             Entering: findMaxHelper(A, left = 1 ,right = 2)

             Leaving: findMaxHelper(A, left = 1 ,right = 2)| returning 3

             Entering: findMaxHelper(A, left = 2 ,right = 3)

             Leaving: findMaxHelper(A, left = 2 ,right = 3)| returning 47

         Leaving: findMaxHelper(A, left = 1 ,right = 3)| returning max2=47

     Leaving: findMaxHelper(A, left = 0 ,right = 3)| returning max2=47

     Entering: findMaxHelper(A, left = 3 ,right = 7)

         Entering: findMaxHelper(A, left = 3 ,right = 5)

             Entering: findMaxHelper(A, left = 3 ,right = 4)

             Leaving: findMaxHelper(A, left = 3 ,right = 4)| returning 91

             Entering: findMaxHelper(A, left = 4 ,right = 5)

             Leaving: findMaxHelper(A, left = 4 ,right = 5)| returning 32

         Leaving: findMaxHelper(A, left = 3 ,right = 5) | returning max1=91

         Entering: findMaxHelper(A, left = 5 ,right = 7)

             Entering: findMaxHelper(A, left = 5 ,right = 6)

             Leaving: findMaxHelper(A, left = 5 ,right = 6)| returning 0

             Entering: findMaxHelper(A, left = 6 ,right = 7)

             Leaving: findMaxHelper(A, left = 6 ,right = 7)| returning 0

         Leaving: findMaxHelper(A, left = 5 ,right = 7)| returning max2=0

     Leaving: findMaxHelper(A, left = 3 ,right = 7) | returning max1=91

 Leaving: findMaxHelper(A, left = 0 ,right = 7)| returning max2=91

And The Answer Is = 91 

findMaxHelper divides the array into half each time, and find the max in left,right: findMaxHelper将数组分为一半,并在左侧,右侧找到最大值:

eg you have array A = [1, 3, 5, 8] , call findMax(A) -> findMaxHelper(A, 0, A.length) : 例如你有数组A = [1, 3, 5, 8] findMax(A) A = [1, 3, 5, 8] ,调用findMax(A) - > findMaxHelper(A, 0, A.length)

     max1 | max2
     1 3  | 5 8

max1|max2 | max1|max2
1   |3    | 5   |8
#include<stdio.h>
#include<stdlib.h>

int high,*a,i=0,n,h;
int max(int *);

int main()
{

    printf("Size of array: ");
    scanf("%d",&n);

    a=(int *)malloc(n*sizeof(int));         //dynamic allocation
    for(i=0;i<n;i++)
    {
        scanf("%d",(a+i));
    }
        i=0;
    high=*a;
    h=max(a);
    printf("The highest element is %d\n",h);
}

int max(int *a)
{

    if(i<n)
    {   
        if(*(a+i)>high)
        {high=*(a+i);}
    i++;
    max(a);                     //recursive call
    }

    return high;
}

Basically finding max in array is not recommended by recursion as it is not required. 基本上,在递归时不建议在数组中查找max,因为它不是必需的。 Divide and conquer algorithms(recursive) are more time costly. 划分和征服算法(递归)的时间成本更高。 But even though if you want to use it, you can use my below algorithm. 但即使你想使用它,你也可以使用我的下面的算法。 Basically, it brings the largest element of array at first position and has almost linear running time.(This algo is just a recursive-illusion though!): 基本上,它在第一个位置带来了最大的数组元素并且具有几乎线性的运行时间。(这个算法只是一个递归错觉!):

        int getRecursiveMax(int arr[], int size){
          if(size==1){
                      return arr[0];
          }else{
                 if(arr[0]< arr[size-1]){
                                      arr[0]=arr[size-1];
                     }
                 return(getRecursiveMax(arr,size-1));
            }

          } 

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

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