-
-
Save gaoyike/e48f4b7ed55e8626e564 to your computer and use it in GitHub Desktop.
islessthan Interview
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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