Recursion

    reading:   Dietel sections 3.12-3.14

Finding the Maximum Number

           the first element   or   the maximum out of the rest of the elements

Recursive Algorithm

        int Maximum( int Array[], int size, int start_position)

        {

            int recursive_max;

            if( size == start_position + 1)  // Only one element

              return Array[start_position];

            recursive_max = Maximum(Array, size, start_position+1);  // Maximum of rest of array

            if( Array[start_position] > recursive_max)

               return Array[start_position];

            else

              return recursive_max;

         }

How does this work?

Recognizing and Solving Recursive Problems

            determines the form of the recursive call

            Maximum of n elements depends on Maximum of n-1 elements           

            we can solve for the Maximum of a one-long list directly

            this is called the base case -- ensures no infinite loops

Recursion vs. Iteration

              faster since no overhead for function calls, stack management

              size of problem not limited by size of stack

              can be easier to trace program execution

               some algorithms can be expressed more simply

               some problems lend themselves to intuitive recursive solutions