Skip to content

Commit d197fc7

Browse files
authored
Add tests, remove main, improve docs in ExponentialSearch (TheAlgorithms#5664)
1 parent a4e4319 commit d197fc7

File tree

3 files changed

+116
-28
lines changed

3 files changed

+116
-28
lines changed

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,6 +1001,7 @@
10011001
* [BM25InvertedIndexTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/BM25InvertedIndexTest.java)
10021002
* [BreadthFirstSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/BreadthFirstSearchTest.java)
10031003
* [DepthFirstSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/DepthFirstSearchTest.java)
1004+
* [ExponentialSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/ExponentialSearchTest.java)
10041005
* [FibonacciSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/FibonacciSearchTest.java)
10051006
* [HowManyTimesRotatedTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/HowManyTimesRotatedTest.java)
10061007
* [KMPSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/KMPSearchTest.java)

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: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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), "The index of the found element should be 6.");
23+
}
24+
25+
/**
26+
* Test for exponential search with the first element as the key.
27+
*/
28+
@Test
29+
void testExponentialSearchFirstElement() {
30+
ExponentialSearch exponentialSearch = new ExponentialSearch();
31+
Integer[] array = {1, 2, 3, 4, 5};
32+
int key = 1; // First element
33+
int expectedIndex = 0; // Index of the key in the array
34+
assertEquals(expectedIndex, exponentialSearch.find(array, key), "The index of the first element should be 0.");
35+
}
36+
37+
/**
38+
* Test for exponential search with the last element as the key.
39+
*/
40+
@Test
41+
void testExponentialSearchLastElement() {
42+
ExponentialSearch exponentialSearch = new ExponentialSearch();
43+
Integer[] array = {1, 2, 3, 4, 5};
44+
int key = 5; // Last element
45+
int expectedIndex = 4; // Index of the key in the array
46+
assertEquals(expectedIndex, exponentialSearch.find(array, key), "The index of the last element should be 4.");
47+
}
48+
49+
/**
50+
* Test for exponential search with a single element present.
51+
*/
52+
@Test
53+
void testExponentialSearchSingleElementFound() {
54+
ExponentialSearch exponentialSearch = new ExponentialSearch();
55+
Integer[] array = {1};
56+
int key = 1; // Only element present
57+
int expectedIndex = 0; // Index of the key in the array
58+
assertEquals(expectedIndex, exponentialSearch.find(array, key), "The index of the single element should be 0.");
59+
}
60+
61+
/**
62+
* Test for exponential search with an empty array.
63+
*/
64+
@Test
65+
void testExponentialSearchEmptyArray() {
66+
ExponentialSearch exponentialSearch = new ExponentialSearch();
67+
Integer[] array = {}; // Empty array
68+
int key = 1; // Key not present
69+
int expectedIndex = -1; // Key not found
70+
assertEquals(expectedIndex, exponentialSearch.find(array, key), "The element should not be found in an empty array.");
71+
}
72+
73+
/**
74+
* Test for exponential search on large array.
75+
*/
76+
@Test
77+
void testExponentialSearchLargeArray() {
78+
ExponentialSearch exponentialSearch = new ExponentialSearch();
79+
Integer[] array = IntStream.range(0, 10000).boxed().toArray(Integer[] ::new); // Array from 0 to 9999
80+
int key = 9999;
81+
int expectedIndex = 9999;
82+
assertEquals(expectedIndex, exponentialSearch.find(array, key), "The index of the last element should be 9999.");
83+
}
84+
}

0 commit comments

Comments
 (0)