Tracing a Recursive Function

Finding the Maximum Value in an Array

  1. Find the maximum in the left half
  2. Find the maximum in the right half
  3. Choose the larger
int maximum(int array[], int left, int right)
{
    // One element in the array (the base case)
  if (left == right)
    return array[left];

  int middle = (left + right) / 2;
  int x = maximum(array, left, middle);      // max in left
  int y = maximum(array, middle + 1, right); // max in right

    // Choose the larger
  if (x > y)
    return x;
  else
    return y;
}
This version shows the function calls in a more readable way:
#include <stdio.h>

int depth = 0;

void indent(int size)
{
  for (int i = 0; i < size; i++)
    printf("    ");
}

int maximum(int array[], int left, int right)
{
  depth++; // to show the indenting

  if (left == right)
  {
    depth--;
    return array[left]; 
  }

  int middle = (left + right) / 2;

  indent(depth);
  printf("calling x = max(%i, %i)\n", left, middle);
  int x = maximum(array, left, middle);

  indent(depth);
  printf("calling y = max(%i, %i)\n", left + 1, right);
  int y = maximum(array, left + 1, right);

  if (x > y)
  {
    depth--;
    return x;
  }
  else
  {
    depth--;
    return y;
  }
}

int main(void)
{
  int a[] = {4, 1, 3, 2};
  int size = sizeof(a) / sizeof(*a);
  
  printf("int a[] = {");
  for (int i = 0; i < size; i++)
  {
    printf("%i", a[i]);
    if (i < size - 1)
      printf(", ");
  }
  printf("}\n\n");

  printf("calling m = max(0, %i)\n", size - 1);
  int max = maximum(a, 0, size - 1);
  printf("Max is %i\n", max);

  return 0;
}
Sample runs:

int a[] = {3, 2}int a[] = {1, 3, 2}int a[] = {4, 1, 3, 2}int a[] = {4, 1, 5, 3, 2}int a[] = {4, 1, 5, 7, 3, 2}
calling m = max(0, 1)
    calling x = max(0, 0)
    calling y = max(1, 1)
Max is 3
calling m = max(0, 2)
    calling x = max(0, 1)
        calling x = max(0, 0)
        calling y = max(1, 1)
    calling y = max(1, 2)
        calling x = max(1, 1)
        calling y = max(2, 2)
Max is 3
calling m = max(0, 3)
    calling x = max(0, 1)
        calling x = max(0, 0)
        calling y = max(1, 1)
    calling y = max(1, 3)
        calling x = max(1, 2)
            calling x = max(1, 1)
            calling y = max(2, 2)
        calling y = max(2, 3)
            calling x = max(2, 2)
            calling y = max(3, 3)
Max is 4
calling m = max(0, 4)
    calling x = max(0, 2)
        calling x = max(0, 1)
            calling x = max(0, 0)
            calling y = max(1, 1)
        calling y = max(1, 2)
            calling x = max(1, 1)
            calling y = max(2, 2)
    calling y = max(1, 4)
        calling x = max(1, 2)
            calling x = max(1, 1)
            calling y = max(2, 2)
        calling y = max(2, 4)
            calling x = max(2, 3)
                calling x = max(2, 2)
                calling y = max(3, 3)
            calling y = max(3, 4)
                calling x = max(3, 3)
                calling y = max(4, 4)
Max is 5
calling m = max(0, 5)
    calling x = max(0, 2)
        calling x = max(0, 1)
            calling x = max(0, 0)
            calling y = max(1, 1)
        calling y = max(1, 2)
            calling x = max(1, 1)
            calling y = max(2, 2)
    calling y = max(1, 5)
        calling x = max(1, 3)
            calling x = max(1, 2)
                calling x = max(1, 1)
                calling y = max(2, 2)
            calling y = max(2, 3)
                calling x = max(2, 2)
                calling y = max(3, 3)
        calling y = max(2, 5)
            calling x = max(2, 3)
                calling x = max(2, 2)
                calling y = max(3, 3)
            calling y = max(3, 5)
                calling x = max(3, 4)
                    calling x = max(3, 3)
                    calling y = max(4, 4)
                calling y = max(4, 5)
                    calling x = max(4, 4)
                    calling y = max(5, 5)
Max is 7