在通过递归阵列查找最大值(Find maximum value in an array by rec

2019-07-03 10:43发布

// 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

我有一个很难理解什么是这个伪代码发生。

有人可以帮助解释什么是在每行发生。 我需要了解此代码之前,我可以回答的问题。

我知道findMax调用辅助函数findMaxHelper的功能,然后findMaxHelper使用递归。 除此之外,我真的不明白。

Answer 1:

您正在使用分而治之算法从阵列中找到的最大元素。 第一你将所述阵列分成单独的元件(除),那么你比较元件(征服)。 您将使用调用数组findMaxHelper递归。

分而治之的总体思路如图所示:

例:

这里max是与您的findMaxHelper有两个参数功能,即leftright

检查这为更多的概念的深入理解的例子。



Answer 2:

捷豹已经把这个概念很好地和保罗提供了正确的和详细的说明。 要添加到这一点,我想和大家分享一个简单的C代码,给你一个想法的代码如何被执行。 以下是捷豹所使用的相同输入的代码:

#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可以复制粘贴乌尔Linux机器上的代码......也许把睡眠(5)每次的printf之后,看看如何递归实际工作...希望这有助于...我也会从我这里系统共享输出!

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 


Answer 3:

findMaxHelper划分数组各占一半时间,并找到在左,右的最大:

例如,你有阵列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


Answer 4:

#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;
}


Answer 5:

基本上发现在阵列最大由于不需要故不推荐通过递归。 分治法(递归)有更多的时间成本。 但是,即使如果你想使用它,你可以用我下面的算法。 基本上,它带来阵列的最大元素在第一位置,并有近线性的运行时间(此算法中仅​​仅是一个递归幻觉虽然!):

        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));
            }

          } 


文章来源: Find maximum value in an array by recursion