public static int[] getMinMax(int[] array) {
int min = Integer.MAX_VALUE; //or -1
int max = Integer.MIN_VALUE; //or -1
for(int i : array) { //if you need to know the index, use int (i=0;i<array.length;i++) instead
if(i < min) min = i; //or if(min == -1 || array[i] < array[min]) min = i;
if(i > max) max = i; //or if(max == -1 || array[i] > array[max]) max = i;
}
return new int[] {min, max};
}
Sorting takes at least O(n log(n)), n being the amount of elements in the array. If you simply look at every element in the array, finding the minimum and maximum element is in O(n). For big arrays, that's a lot faster.