简体   繁体   中英

Find maximal element of an array recursively

I have this function:

int max(int arr[], int size)
{
    size--;
    if(size > 0)
    {
        int max = max(arr, size);
        if(arr[max] > arr[size]) return max;
    }
    return size;
}

And of course it works. My question is - how does this work? Could anyone explain me this step by step? It's Saturday so maybe somebody has a bit of time :DI especially mean these two lines in if block.

Let's take this array for example:

int arr[] = { 42, 54, 23 };
max(arr, 3);

This function work by checking the max of the current element against the max of all previous elements. Here is a representation of the stack calls:

max(arr, 3)
    max(arr, 2)
        max(arr, 1)
            return 0
        arr[0] (arr[max] = 42) <= arr[0] (arr[size] = 42) => return size (0)
    arr[0] (arr[max] = 42) <= arr[1] (arr[size] = 54) => return size (1)
arr[1] (arr[max] = 54) > arr[2] (arr[size] = 23) => return max (1)

NB: It is a bad idea to name both a variable and a function with the same identifier.

You code is as follows:

1 int max(int arr[], int size){
2     size--;
3     if(size > 0){
4         int max = max(arr, size);
5         if(arr[max] > arr[size]) return max;
6     }
7     return size;
8 }

You call it by passing it array and the size (or length) of that array.

The first important point the code hits is on Line 4, when it calls itself recursively. But notice that on Line 2, the size was decreased by one. Therefore, you can think of size as being an index referring to the element of the array being considered by the current call to the function.

This means that eventually, the size of the array will be down to zero and we will be looking at the first element of the array. At this point Lines 3 through 6 are skipped, and 0 is returned.

When one of the recursive calls returns, we're left back at Line 4.

For the first return, this means that int max = 0; .

Now we are comparing the zeroth element with the first element. We return the index of whichever is greater. The next comparison will be between the second element and whichever of the first two was larger.

This continues until all the recursive calls are returned, at which point the index of the greatest element is returned to the calling function.

Note then, that return size; should be replaced with return 0 to increase clarity.

Lets step through the problem for arr[] = [1,2] which means call max(arr[], 2)

size--; // 2-1 = 1
if(size > 0) //True
    {
        int max = max(arr, size); //Going into the recursive call 

This is the recursive run :

    size--; // Now 1-1 = 0
    if(size > 0) //False
    return size; //0 returned

Now back into the calling function :

        int max = 0 //From return
        if(arr[max] > arr[size]) //arr[0]>arr[1] which is false 
    }
    return size; //Now, control reaches here and 1 is passed back to the calling scope
}

在此输入图像描述

Sorry for the poor formatting of the diagram

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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