Skip to content

Instantly share code, notes, and snippets.

@thmain
Created November 15, 2015 01:16
Show Gist options
  • Save thmain/efbeb16b79d95d23bd3b to your computer and use it in GitHub Desktop.
Save thmain/efbeb16b79d95d23bd3b to your computer and use it in GitHub Desktop.
public class MagicIndex {
// perform modified binary search
public int search(int[] A, int start, int end) {
if (start <= end) {
int mid = (start + end) / 2;
if (mid == A[mid]) // check for magic index.
return mid;
if (mid > A[mid]) { // If mid>A[mid] means fixed point might be on
// the right half of the array
return search(A, mid + 1, end);
} else {// If mid<A[mid] means fixed point might be on
// the left half of the array
return search(A, start, mid - 1);
}
}
return -1;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] A = { -1, 0, 1, 2, 4, 10 };
MagicIndex m = new MagicIndex();
System.out.println("Magic index " + m.search(A, 0, A.length - 1));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment