diff --git a/algorithms/Java/searching/order-agnostic-binary-search.java b/algorithms/Java/searching/order-agnostic-binary-search.java new file mode 100644 index 00000000..f3bc45c3 --- /dev/null +++ b/algorithms/Java/searching/order-agnostic-binary-search.java @@ -0,0 +1,47 @@ +//Order Agnostic Binary Search implementation in JAVA + +public class OrderAgnosticBinarySearch { + + static int orderAgnosticBinarySearch(int[] arr, int target) { + int start = 0; + int end = arr.length - 1; + + // find whether the array is sorted in ascending or descending + boolean isAsc = arr[start] < arr[end]; + + while(start <= end) { + // find the middle element +// int mid = (start + end) / 2; +// might be possible that (start + end) exceeds the range of int in java + int mid = start + (end - start) / 2; + + if (arr[mid] == target) { + return mid; + } + + if (isAsc) { + if (target < arr[mid]) { + end = mid - 1; + } else { + start = mid + 1; + } + } else { + if (target > arr[mid]) { + end = mid - 1; + } else { + start = mid + 1; + } + } + } + return -1; + } + + public static void main(String[] args) { + int[] arr = {1, 5, 10, 20, 30}; + int target = 30; + int ans = orderAgnosticBinarySearch(arr, target); + System.out.println(ans); + } +} + +//rename to OrderAgnosticBinarySearch to run. \ No newline at end of file