| 2 | import java.util.Arrays; |
| 3 | |
| 4 | class ExponentialSearch |
| 5 | { |
| 6 | |
| 7 | static int exponentialSearch(int arr[], |
| 8 | int n, int x) |
| 9 | { |
| 10 | if (arr[0] == x) |
| 11 | return 0; |
| 12 | |
| 13 | int i = 1; |
| 14 | while (i < n && arr[i] <= x) |
| 15 | i = i*2; |
| 16 | |
| 17 | return Arrays.binarySearch(arr, i/2, |
| 18 | Math.min(i, n-1), x); |
| 19 | } |
| 20 | |
| 21 | public static void main(String args[]) |
| 22 | { |
| 23 | int arr[] = {2, 3, 4, 10, 40}; |
| 24 | int x = 10; |
| 25 | int result = exponentialSearch(arr, |
| 26 | arr.length, x); |
| 27 | |
| 28 | System.out.println((result < 0) ? |
| 29 | "Element is not present in array" : |
| 30 | "Element is present at index " + |
| 31 | result); |
| 32 | } |
| 33 | } |
nothing calls this directly
no outgoing calls
no test coverage detected