So I'm trying to write a recursive algorithm which finds the min and max of an array. It basically splits the array into two n/2 pieces and finds the min and max recursively. Here's what I have right now:
class MinMax{
public static Pair mmA(int lb, int ub, int[] a){
int min;
int max;
int mid;
if (ub == lb){
return mmA(lb, ub, a);
} else {
mid = (lb + ub)/2;
mmA (lb, mid, a);
max = mid;
mmA (mid+1, ub, a);
min = mid;
if (a[max] > a[min]){
return mmA(lb, max, a);
} else
return mmA(min, ub, a);
}
}
public static void main(String[]args){
int[] a = {4, 3, 5, 6, 7, 9, 1};
mmA(0, 6, a);
}
}
The problem is the method is not an int method so I can't just say max = mmA(lb, mid, a) because mmA is a Pair method while max is an int. I also can't just make Max and Min pair objects because then you wouldn't be able to compare them at the end. Here's the pair class:
class Pair {
int alpha; // the smaller one
int omega; // the bigger one
Pair ( int a, int o ) { alpha = a; omega = o; }
}
So how can I use this pair class along with my method to find the min and max.