mercredi 4 mars 2015

Java: Binary Search Implementation not working using Deferred detection of equality



I am trying to implement Binary Search Java version. From wikipedia http://ift.tt/1w3a3Y5 I noticed that deferred detection of equality version. It's working using that algorithm. However, when I was trying to change the if condition expression like this:



public int bsearch1(int[] numbers, int key, int start){
int L = start, R = numbers.length - 1;
while(L < R){
//find the mid point value
int mid = (L + R) / 2;
if (numbers[mid] >key){
//move to left
R = mid - 1;
} else{
// move to right, here numbers[mid] <= key
L = mid;
}
}
return (L == R && numbers[L] == key) ? L : -1;
}


It's not working properly, which goes into an infinity loop. Do you guys have any ideas about it? Thank you so much.




Aucun commentaire:

Enregistrer un commentaire