[英]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
我很難理解這個偽代碼中發生了什么。
有人可以幫助解釋每一行發生的事情。 在我回答問題之前,我需要先理解這段代碼。
我知道函數findMax調用輔助函數findMaxHelper,然后findMaxHelper使用遞歸。 除此之外,我真的不明白。
您正在使用Divide and Conquer算法來查找數組中的最大元素。 首先,您將數組划分為單個元素(除法),然后比較元素(征服)。 您正在使用遞歸調用findMaxHelper
來划分數組。
分而治之的總體思路如下圖所示:
例:
這里
max
與findMaxHelper
函數相同,有兩個參數,即left
和right
。
查看此示例以更深入地了解該概念。
捷豹已經很好地理解了這個概念,保羅提供了正確而詳細的解釋。 除此之外,我想分享一個簡單的C代碼,讓您了解代碼是如何執行的。 這是使用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);
}
你可以復制粘貼你的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
將數組分為一半,並在左側,右側找到最大值:
例如你有數組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;
}
基本上,在遞歸時不建議在數組中查找max,因為它不是必需的。 划分和征服算法(遞歸)的時間成本更高。 但即使你想使用它,你也可以使用我的下面的算法。 基本上,它在第一個位置帶來了最大的數組元素並且具有幾乎線性的運行時間。(這個算法只是一個遞歸錯覺!):
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.