As noted in the comments by Danny-ds, if you really need to do this using recursion (for didactic purpose), at least use a divide and conquer technique to limit the stack consumption to O(ln(n)).
In the following I'll create a function which accepts a range defined as a pointer first to the first element of an array (or subarray) and a pointer last to past one the last element of the same array (or subarray). Note that those two pointer can be safely compared, but last shouldn't be dereferenced.
This function returns a struct which aggregates two pointers to the min and max values. If the passed array is empty (when first == last, with this convention) those pointers are set to last and this condition should be checked before using the struct.
#include <stdio.h>
typedef struct {
int *min;
int *max;
} min_max_t;
min_max_t min_max_element(int *first, int *last)
{
// First check if the range is at least two elements wide, to stop the recursion.
if ( first == last || first + 1 == last )
return (min_max_t){first, first};
// Then apply the algorithm to two sub-range
int *middle = first + (last - first)/2;
min_max_t left = min_max_element(first, middle);
min_max_t right = min_max_element(middle, last);
// No need to compare 'right.min' with 'left.max' or 'right.max' with 'left.min'
if ( *(right.min) < *(left.min) )
left.min = right.min;
if ( *(right.max) > *(left.max) )
left.max = right.max;
return left;
}
// Helper function to print the result. The only part which is really necessary is
// the check before accessing the resulting struct.
void print_min_max(size_t n, int *arr)
{
int *arr_end = arr + n;
min_max_t result = min_max_element(arr, arr_end);
if (result.min != arr_end)
printf("Min: %d\tMax: %d\n", *(result.min), *(result.max));
else
puts("Error: invalid array.");
}
int main(void)
{
int a1[] = { 1, 2, 3, 4, -5 };
print_min_max(5, a1); // -> Min: -5 Max: 4
int a2[] = { 4, 2, 1, 4, -2, 0 };
print_min_max(6, a2); // -> Min: -2 Max: 4
int *a3 = NULL;
print_min_max(0, a3); // -> Error: invalid array.
int a4[] = { 1 };
print_min_max(1, a4); // -> Min: 1 Max: 1
int a5[] = { 2, 2, 2, 2 };
print_min_max(4, a5); // -> Min: 2 Max: 2
}