Skip to content

Commit 3c3f04e

Browse files
hardikkauranialxkm
andauthored
fix(searches): add null safety and make ExponentialSearch public (#7651)
Co-authored-by: Oleksandr Klymenko <19151554+alxkm@users.noreply.github.com>
1 parent 18c99d9 commit 3c3f04e

2 files changed

Lines changed: 57 additions & 4 deletions

File tree

‎src/main/java/com/thealgorithms/searches/ExponentialSearch.java‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* Note: This algorithm requires that the input array be sorted.
2020
* </p>
2121
*/
22-
class ExponentialSearch implements SearchAlgorithm {
22+
public final class ExponentialSearch implements SearchAlgorithm {
2323

2424
/**
2525
* Finds the index of the specified key in a sorted array using exponential search.
@@ -31,13 +31,13 @@ class ExponentialSearch implements SearchAlgorithm {
3131
*/
3232
@Override
3333
public <T extends Comparable<T>> int find(T[] array, T key) {
34-
if (array.length == 0) {
34+
if (array == null || array.length == 0 || key == null) {
3535
return -1;
3636
}
37-
if (array[0].equals(key)) {
37+
if (key.compareTo(array[0]) == 0) {
3838
return 0;
3939
}
40-
if (array[array.length - 1].equals(key)) {
40+
if (key.compareTo(array[array.length - 1]) == 0) {
4141
return array.length - 1;
4242
}
4343

‎src/test/java/com/thealgorithms/searches/ExponentialSearchTest.java‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.thealgorithms.searches;
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertTrue;
45

56
import java.util.stream.IntStream;
67
import org.junit.jupiter.api.Test;
@@ -58,6 +59,17 @@ void testExponentialSearchSingleElementFound() {
5859
assertEquals(expectedIndex, exponentialSearch.find(array, key), "The index of the single element should be 0.");
5960
}
6061

62+
/**
63+
* Test for exponential search with a single element not present.
64+
*/
65+
@Test
66+
void testExponentialSearchSingleElementNotFound() {
67+
ExponentialSearch exponentialSearch = new ExponentialSearch();
68+
Integer[] array = {1};
69+
int key = 2; // Key not present
70+
assertEquals(-1, exponentialSearch.find(array, key), "The element should not be found in the array.");
71+
}
72+
6173
/**
6274
* Test for exponential search with an empty array.
6375
*/
@@ -70,6 +82,25 @@ void testExponentialSearchEmptyArray() {
7082
assertEquals(expectedIndex, exponentialSearch.find(array, key), "The element should not be found in an empty array.");
7183
}
7284

85+
/**
86+
* Test for exponential search with a null array.
87+
*/
88+
@Test
89+
void testExponentialSearchNullArray() {
90+
ExponentialSearch exponentialSearch = new ExponentialSearch();
91+
assertEquals(-1, exponentialSearch.find(null, 5), "Should return -1 for null array.");
92+
}
93+
94+
/**
95+
* Test for exponential search with a null key.
96+
*/
97+
@Test
98+
void testExponentialSearchNullKey() {
99+
ExponentialSearch exponentialSearch = new ExponentialSearch();
100+
Integer[] array = {1, 2, 3, 4, 5};
101+
assertEquals(-1, exponentialSearch.find(array, null), "Should return -1 for null key.");
102+
}
103+
73104
/**
74105
* Test for exponential search on large array.
75106
*/
@@ -123,4 +154,26 @@ void testExponentialSearchNotFoundReturnsMinusOne() {
123154
assertEquals(-1, exponentialSearch.find(array, 0), "A key below the minimum should give -1.");
124155
assertEquals(-1, exponentialSearch.find(array, 12), "A key above the maximum should give -1.");
125156
}
157+
158+
/**
159+
* Test for duplicate elements in the array.
160+
*/
161+
@Test
162+
void testExponentialSearchDuplicates() {
163+
ExponentialSearch exponentialSearch = new ExponentialSearch();
164+
Integer[] array = {1, 2, 2, 2, 3};
165+
int index = exponentialSearch.find(array, 2);
166+
assertTrue(index >= 1 && index <= 3, "Index should be one of the occurrences of 2.");
167+
}
168+
169+
/**
170+
* Test for generic Comparable types such as Strings.
171+
*/
172+
@Test
173+
void testExponentialSearchStrings() {
174+
ExponentialSearch exponentialSearch = new ExponentialSearch();
175+
String[] words = {"apple", "banana", "cherry", "date", "elderberry"};
176+
assertEquals(2, exponentialSearch.find(words, "cherry"), "Should find 'cherry' at index 2.");
177+
assertEquals(-1, exponentialSearch.find(words, "fig"), "Should return -1 for absent word.");
178+
}
126179
}

0 commit comments

Comments
 (0)