public class ArrayMaxTest { public static void main(String[] args) { int values[] = {11,69,29,127,22,15,40,80,5,43,8,2,101,99,71}; int large; large = ArrayMax.maxArray(values,15); System.out.println("Largest number in the array: " + large); } } public class ArrayMax { // finds and returns max value in arr with size elements // size is number of elements in arr // largest value in arr is returned // array is unchanged public static int maxArray(int arr[], int size) { return maxArray(arr, 0, size); } private static int maxArray(int arr[], int pos, int size) { int maxbot, maxtop; if (size == 1) return arr[pos]; else { maxbot=maxArray(arr,pos,size/2); maxtop=maxArray(arr,pos+size/2,size-size/2); if (maxbot>maxtop) return maxbot; else return maxtop; } } }
Largest number in the array: 127
Email Me |
Office Hours |
My Home Page |
Department Home |
MCC Home Page
© Copyright Emmi Schatz 2008