Skip to content

Instantly share code, notes, and snippets.

@bittib
Created May 13, 2013 08:59
Show Gist options
  • Save bittib/5567049 to your computer and use it in GitHub Desktop.
Save bittib/5567049 to your computer and use it in GitHub Desktop.
find the minimum and maximum element from an array with minimum time of comparision
/**
* Time Complexity : Comparision time : 3 * n/2 + 1
*/
public int[] findMinMax(int[] A){
int n = A.length;
if (n == 0) return null;
int min = Integer.MAX_VALUE, max = Integer.MIN_VALUE;
for (int i=0; i < n-1; i+=2){
int smaller = A[i], bigger = A[i+1];
if (A[i] > A[i+1){
smaller = A[i+1];
bigger = A[i];
}
if (smaller < min)
min = smaller;
if (bigger > max)
max = bigger;
}
if (n % 2 == 1){
if (A[n-1] < min)
min = A[n-1];
else (A[n-1] > max)
max = A[n-1];
}
return new int[]{min, max};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment