Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static<T extends Comparable<? super T>> int binarySearch(T[] A, int p, int r, T x) {
- //System.out.println("Element from array "+A[r]+"value to be compared "+ x);
- //System.out.println(A[p]+" "+x);
- int z=A[r].compareTo(x);
- int t=A[p].compareTo(x);
- if(z!=1||t!=-1){
- System.out.println(x+" not within "+A[p] + " "+A[r]);
- return(r+1);
- }else{
- while(p <= r) {
- int q = (p+r) >>> 1;
- int cmp = A[q].compareTo(x);
- if (cmp < 0) {
- p = q+1;
- } else if (cmp == 0) { // x found
- return q;
- } else { // A[q] > x, so x is not in the right half
- r = q-1;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement