Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class BinarySearchRecursive {
- public static int search(int[] arr, int start, int end, int num) {
- if (end - start == 1) {
- if (arr[start] == num) {
- return start;
- } else if (arr[end] == num) {
- return end;
- } else {
- return -1;
- }
- } else {
- int mid = (start + end) / 2;
- if (arr[mid] == num) {
- return num;
- } else {
- if (arr[mid] < num) {
- return search(arr, mid, end, num);
- } else {
- return search(arr, start, mid - 1, num);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement