Skip to content

Commit af079f9

Browse files
committed
Add tests, remove main, improve docs in ExponentialSearch
1 parent b54cc21 commit af079f9

File tree

2 files changed

+121
-28
lines changed

2 files changed

+121
-28
lines changed

src/main/java/com/thealgorithms/searches/ExponentalSearch.java

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,47 @@
22

33
import com.thealgorithms.devutils.searches.SearchAlgorithm;
44
import java.util.Arrays;
5-
import java.util.Random;
6-
import java.util.concurrent.ThreadLocalRandom;
7-
import java.util.stream.IntStream;
85

6+
/**
7+
* ExponentialSearch is an algorithm that efficiently finds the position of a target
8+
* value within a sorted array. It works by expanding the range to find the bounds
9+
* where the target might exist and then using binary search within that range.
10+
*
11+
* <p>
12+
* Worst-case time complexity: O(log n)
13+
* Best-case time complexity: O(1) when the element is found at the first position.
14+
* Average time complexity: O(log n)
15+
* Worst-case space complexity: O(1)
16+
* </p>
17+
*
18+
* <p>
19+
* Note: This algorithm requires that the input array be sorted.
20+
* </p>
21+
*/
922
class ExponentialSearch implements SearchAlgorithm {
1023

11-
public static void main(String[] args) {
12-
Random r = ThreadLocalRandom.current();
13-
14-
int size = 100;
15-
int maxElement = 100000;
16-
17-
Integer[] integers = IntStream.generate(() -> r.nextInt(maxElement)).limit(size).sorted().boxed().toArray(Integer[] ::new);
18-
19-
// The element that should be found
20-
int shouldBeFound = integers[r.nextInt(size - 1)];
21-
22-
ExponentialSearch search = new ExponentialSearch();
23-
int atIndex = search.find(integers, shouldBeFound);
24-
25-
System.out.printf("Should be found: %d. Found %d at index %d. An array length %d%n", shouldBeFound, integers[atIndex], atIndex, size);
26-
27-
int toCheck = Arrays.binarySearch(integers, shouldBeFound);
28-
System.out.printf("Found by system method at an index: %d. Is equal: %b%n", toCheck, toCheck == atIndex);
29-
}
30-
24+
/**
25+
* Finds the index of the specified key in a sorted array using exponential search.
26+
*
27+
* @param array The sorted array to search.
28+
* @param key The element to search for.
29+
* @param <T> The type of the elements in the array, which must be comparable.
30+
* @return The index of the key if found, otherwise -1.
31+
*/
3132
@Override
3233
public <T extends Comparable<T>> int find(T[] array, T key) {
33-
if (array[0] == key) {
34+
if (array.length == 0) {
35+
return -1;
36+
}
37+
if (array[0].equals(key)) {
3438
return 0;
3539
}
36-
if (array[array.length - 1] == key) {
37-
return array.length;
40+
if (array[array.length - 1].equals(key)) {
41+
return array.length - 1;
3842
}
3943

4044
int range = 1;
41-
42-
while (range < array.length && array[range].compareTo(key) <= -1) {
45+
while (range < array.length && array[range].compareTo(key) < 0) {
4346
range = range * 2;
4447
}
4548

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package com.thealgorithms.searches;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.util.stream.IntStream;
6+
import org.junit.jupiter.api.Test;
7+
8+
/**
9+
* Unit tests for the ExponentialSearch class.
10+
*/
11+
class ExponentialSearchTest {
12+
13+
/**
14+
* Test for basic exponential search functionality.
15+
*/
16+
@Test
17+
void testExponentialSearchFound() {
18+
ExponentialSearch exponentialSearch = new ExponentialSearch();
19+
Integer[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
20+
int key = 7;
21+
int expectedIndex = 6; // Index of the key in the array
22+
assertEquals(expectedIndex, exponentialSearch.find(array, key),
23+
"The index of the found element should be 6.");
24+
}
25+
26+
/**
27+
* Test for exponential search with the first element as the key.
28+
*/
29+
@Test
30+
void testExponentialSearchFirstElement() {
31+
ExponentialSearch exponentialSearch = new ExponentialSearch();
32+
Integer[] array = {1, 2, 3, 4, 5};
33+
int key = 1; // First element
34+
int expectedIndex = 0; // Index of the key in the array
35+
assertEquals(expectedIndex, exponentialSearch.find(array, key),
36+
"The index of the first element should be 0.");
37+
}
38+
39+
/**
40+
* Test for exponential search with the last element as the key.
41+
*/
42+
@Test
43+
void testExponentialSearchLastElement() {
44+
ExponentialSearch exponentialSearch = new ExponentialSearch();
45+
Integer[] array = {1, 2, 3, 4, 5};
46+
int key = 5; // Last element
47+
int expectedIndex = 4; // Index of the key in the array
48+
assertEquals(expectedIndex, exponentialSearch.find(array, key),
49+
"The index of the last element should be 4.");
50+
}
51+
52+
/**
53+
* Test for exponential search with a single element present.
54+
*/
55+
@Test
56+
void testExponentialSearchSingleElementFound() {
57+
ExponentialSearch exponentialSearch = new ExponentialSearch();
58+
Integer[] array = {1};
59+
int key = 1; // Only element present
60+
int expectedIndex = 0; // Index of the key in the array
61+
assertEquals(expectedIndex, exponentialSearch.find(array, key),
62+
"The index of the single element should be 0.");
63+
}
64+
65+
/**
66+
* Test for exponential search with an empty array.
67+
*/
68+
@Test
69+
void testExponentialSearchEmptyArray() {
70+
ExponentialSearch exponentialSearch = new ExponentialSearch();
71+
Integer[] array = {}; // Empty array
72+
int key = 1; // Key not present
73+
int expectedIndex = -1; // Key not found
74+
assertEquals(expectedIndex, exponentialSearch.find(array, key),
75+
"The element should not be found in an empty array.");
76+
}
77+
78+
/**
79+
* Test for exponential search on large array.
80+
*/
81+
@Test
82+
void testExponentialSearchLargeArray() {
83+
ExponentialSearch exponentialSearch = new ExponentialSearch();
84+
Integer[] array = IntStream.range(0, 10000).boxed().toArray(Integer[]::new); // Array from 0 to 9999
85+
int key = 9999; // Last element
86+
int expectedIndex = 9999; // Index of the last element
87+
assertEquals(expectedIndex, exponentialSearch.find(array, key),
88+
"The index of the last element should be 9999.");
89+
}
90+
}

0 commit comments

Comments
 (0)