From 4fcad4fcbc31cc43b91789260228878f01a47676 Mon Sep 17 00:00:00 2001 From: CodingCookieRookie <38324769+CodingCookieRookie@users.noreply.github.com> Date: Tue, 19 May 2020 23:20:48 +0800 Subject: [PATCH] Handles all corner cases --- Searches/Perfect BinarySearch | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 Searches/Perfect BinarySearch diff --git a/Searches/Perfect BinarySearch b/Searches/Perfect BinarySearch new file mode 100644 index 00000000..28cdcb5b --- /dev/null +++ b/Searches/Perfect BinarySearch @@ -0,0 +1,21 @@ + static int binarySearch(int[] arr, int target) { + int low = 0 ; + int high = arr.length - 1 ; + + while(low <= high) { + int mid =(low + high) / 2; + + if(arr[mid] == target) { + return mid; + } + else if(arr[mid] > target) { + high = mid - 1; + } + else { + low = mid + 1; + } + + } + return -1; + } +