Skip to content

Instantly share code, notes, and snippets.

@linstantnoodles
Created February 10, 2013 21:04
Show Gist options
  • Save linstantnoodles/4751047 to your computer and use it in GitHub Desktop.
Save linstantnoodles/4751047 to your computer and use it in GitHub Desktop.
public static int findElement(int[] a, int l, int r, int x){
int m = (l+r)/2;
int t = -1;
if(x == a[m]) return m;
if(l < r){
if(a[l] == a[m]){
if(x == a[l])
t = l;
else
t = findElement(a, m + 1, r, x);
}else if(a[l] < a[m]){
if(x == a[l])
t = l;
else if(x < a[m] && x > a[l])
t = findElement(a, l + 1, m - 1 , x);
else
t = findElement(a, m + 1, r, x);
}else{
if(x == a[r])
t = r;
else if(x > a[m] && x < a[r])
t = findElement(a, m + 1, r - 1, x);
else
t = findElement(a, l, m - 1, x);
}
}
return t;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment