diff --git a/Exponential Search b/Exponential Search new file mode 100644 index 0000000..6aefb14 --- /dev/null +++ b/Exponential Search @@ -0,0 +1,36 @@ +public class ExponentialSearch { + public static int exponentialSearch(int[] arr, int target) { + int bound = 1; + while (bound < arr.length && arr[bound] < target) { + bound *= 2; + } + int left = bound / 2; + int right = Math.min(bound, arr.length - 1); + return binarySearch(arr, target, left, right); + } + + public static int binarySearch(int[] arr, int target, int left, int right) { + while (left <= right) { + int mid = left + (right - left) / 2; + if (arr[mid] == target) { + return mid; + } else if (arr[mid] < target) { + left = mid + 1; + } else { + right = mid - 1; + } + } + return -1; + } + + public static void main(String[] args) { + int[] arr = {2, 5, 8, 12, 16, 23, 38, 42, 56, 72, 91}; + int target = 23; + int index = exponentialSearch(arr, target); + if (index != -1) { + System.out.println("Element found at index: " + index); + } else { + System.out.println("Element not found in the array."); + } + } +}