Created
August 11, 2019 18:24
-
-
Save RitamChakraborty/5d8b2247706c807ebd3b5be9fb8ecdad to your computer and use it in GitHub Desktop.
Binary Search in recursive way
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment