I'm trying to convert this recursive method to an iterative and I'm a bit stuck as my book doesn't explain it enough. This method searches an array between two values for a specific value and returns the index. Any help or a point in the right direction would be appreciated.
public static int binarySearch(int anArray[], int first, int last, int value) {
int index;
if (first > last) {
index = -1;
} else {
int mid = (first + last) / 2;
if(value == anArray[mid]) {
index = mid;
} else if(value < anArray[mid]) { //Point x
index = binarySearch(anArray, first, mid - 1, value);
} else { //Point Y
index = binarySearch(anArray, mid + 1, last, value);
}
}
return index;
}