-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathexponentialSearch.java
More file actions
69 lines (56 loc) · 1.86 KB
/
exponentialSearch.java
File metadata and controls
69 lines (56 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
class Main {
private static int binarySearch(int[] A, int left, int right, int x)
{
// base condition (search space is exhausted)
if (left > right) {
return -1;
}
// find the mid-value in the search space and
// compares it with the key
int mid = (left + right) / 2;
// overflow can happen. Use
// int mid = left + (right - left) / 2;
// base condition (a key is found)
if (x == A[mid]) {
return mid;
}
// discard all elements in the right search space,
// including the middle element
else if (x < A[mid]) {
return binarySearch(A, left, mid - 1, x);
}
// discard all elements in the left search space,
// including the middle element
else {
return binarySearch(A, mid + 1, right, x);
}
}
// Returns the position of key `x` in a given array `A` of length `n`
public static int exponentialSearch(int[] A, int x)
{
// base case
if (A == null || A.length == 0) {
return -1;
}
int bound = 1;
// find the range in which key `x` would reside
while (bound < A.length && A[bound] < x) {
bound *= 2; // calculate the next power of 2
}
// call binary search on A[bound/2 … min(bound, n-1)]
return binarySearch(A, bound/2, Integer.min(bound, A.length - 1), x);
}
// Exponential search algorithm
public static void main(String[] args)
{
int[] A = {2, 5, 6, 8, 9, 10};
int key = 9;
int index = exponentialSearch(A, key);
if (index != -1) {
System.out.println("Element found at index " + index);
}
else {
System.out.println("Element not found in the array");
}
}
}