Skip to content

Commit da5fd85

Browse files
Exponnetial search algorithm
1 parent 3ed3052 commit da5fd85

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@
22

33
* Binary Search Algorithms (Array must be sorted first)
44
* Using Recursion
5-
* Using Iteration
5+
* Using Iteration
6+
* Ternary Search Algorithm
7+
* Jump Search Algorithm
8+
* Exponential Search Algorithm

src/main/java/org/example/Main.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public static void main(String[] args) {
99

1010
Arrays.sort(array);
1111
var search = new Search();
12-
var result = search.jumpSearch(array, 56);
12+
var result = search.exponentialSearch(array, 56);
1313

1414
System.out.println(result);
1515
}

src/main/java/org/example/Search.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,18 @@ public int jumpSearch(int[] array, int target) {
7777
return i;
7878
return -1;
7979
}
80+
81+
public int exponentialSearch(
82+
int[] array, int target) {
83+
84+
int bound = 1;
85+
while (bound < array.length &&
86+
array[bound] < target)
87+
bound *= 2;
88+
89+
int left = bound / 2;
90+
int right = Math.min(bound, array.length - 1);
91+
92+
return binarySearchRecursion(array, target, left, right);
93+
}
8094
}

0 commit comments

Comments
 (0)