Skip to content

Instantly share code, notes, and snippets.

@gaoyike
Created July 22, 2014 07:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gaoyike/e48f4b7ed55e8626e564 to your computer and use it in GitHub Desktop.
Save gaoyike/e48f4b7ed55e8626e564 to your computer and use it in GitHub Desktop.
islessthan Interview
/**
* Created by Readman on 7/22/14.
*/
public class IsLessThan {
int x;
public int BS(int[] A, int x){
this.x = x;
return rec(A, x, 0, A.length-1);
}
public int rec(int[] A, int x, int start, int end) {
if (start > end)
return -1;
int mid = start + (end - start) / 2;
if (A[mid] == x)
return mid;
if (isLessThanX(A[mid])){
return rec(A, x, mid+1, end);
} else{
return rec(A, x, start, mid-1);
}
}
public boolean isLessThanX(int y){
return y<x;
}
public static void main(String[] args) {
IsLessThan isLessThan = new IsLessThan();
System.out.println(isLessThan.BS(new int[]{1,3,5,7,9}, 7));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment