public class Test5 {
public static void main(String[] args) {
int[] arr = { 1, 2, 3, 4, 5, 6, 7 };
System.out.println(sort(arr, 0, arr.length - 1, 4));
}
public static int sort(int[] arr, int start, int end, int key) {
int mid = (start + end) / 2;
if (arr[mid] == key) {
return mid;
} else if (key > arr[mid] && end >= 0 && start <= arr.length - 1) {
start = mid + 1;
return sort(arr, start, end, key);
} else if (key < arr[mid] && end >= 0 && start <= arr.length - 1) {
end = mid - 1;
return sort(arr, start, end, key);
} else {
return -1;
}
}
}
|