Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class App {
- public static void main(String[] args) {
- System.out.println(recursiveBinarySearch(new int[]{1, 2, 3, 4, 7, 9, 12, 18}, 0, 7, 6));
- // System.out.println(binarySearch(new int[]{3, 11, 21, 29, 41, 54, 61, 78, 110, 127}, 54));
- }
- public static int recursiveBinarySearch(int[] arr, int first, int last, int value) {
- if (first > last) {
- return -1;
- }
- int mid = (first + last) / 2;
- if (arr[mid] == value) {
- return mid;
- } else if (arr[mid] > value) {
- return recursiveBinarySearch(arr, first, mid - 1, value);
- } else {
- return recursiveBinarySearch(arr, mid + 1, last, value);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement