Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.49 KB | None | 0 0
  1. public class BinarySearchRecursive {
  2. public static int search(int[] arr, int start, int end, int num) {
  3. if (end - start == 1) {
  4. if (arr[start] == num) {
  5. return start;
  6. } else if (arr[end] == num) {
  7. return end;
  8. } else {
  9. return -1;
  10. }
  11. } else {
  12. int mid = (start + end) / 2;
  13.  
  14. if (arr[mid] == num) {
  15. return num;
  16. } else {
  17. if (arr[mid] < num) {
  18. return search(arr, mid, end, num);
  19. } else {
  20. return search(arr, start, mid - 1, num);
  21. }
  22. }
  23. }
  24. }
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement