diff --git a/src/main/java/com/thealgorithms/searches/ExponentialSearch.java b/src/main/java/com/thealgorithms/searches/ExponentialSearch.java index e666b9148aaa..9405f5995687 100644 --- a/src/main/java/com/thealgorithms/searches/ExponentialSearch.java +++ b/src/main/java/com/thealgorithms/searches/ExponentialSearch.java @@ -19,7 +19,7 @@ * Note: This algorithm requires that the input array be sorted. *

*/ -class ExponentialSearch implements SearchAlgorithm { +public final class ExponentialSearch implements SearchAlgorithm { /** * Finds the index of the specified key in a sorted array using exponential search. @@ -31,13 +31,13 @@ class ExponentialSearch implements SearchAlgorithm { */ @Override public > int find(T[] array, T key) { - if (array.length == 0) { + if (array == null || array.length == 0 || key == null) { return -1; } - if (array[0].equals(key)) { + if (key.compareTo(array[0]) == 0) { return 0; } - if (array[array.length - 1].equals(key)) { + if (key.compareTo(array[array.length - 1]) == 0) { return array.length - 1; } diff --git a/src/test/java/com/thealgorithms/searches/ExponentialSearchTest.java b/src/test/java/com/thealgorithms/searches/ExponentialSearchTest.java index c6b07ca2b4d5..ed6f0f801bf4 100644 --- a/src/test/java/com/thealgorithms/searches/ExponentialSearchTest.java +++ b/src/test/java/com/thealgorithms/searches/ExponentialSearchTest.java @@ -1,6 +1,7 @@ package com.thealgorithms.searches; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.stream.IntStream; import org.junit.jupiter.api.Test; @@ -58,6 +59,17 @@ void testExponentialSearchSingleElementFound() { assertEquals(expectedIndex, exponentialSearch.find(array, key), "The index of the single element should be 0."); } + /** + * Test for exponential search with a single element not present. + */ + @Test + void testExponentialSearchSingleElementNotFound() { + ExponentialSearch exponentialSearch = new ExponentialSearch(); + Integer[] array = {1}; + int key = 2; // Key not present + assertEquals(-1, exponentialSearch.find(array, key), "The element should not be found in the array."); + } + /** * Test for exponential search with an empty array. */ @@ -70,6 +82,25 @@ void testExponentialSearchEmptyArray() { assertEquals(expectedIndex, exponentialSearch.find(array, key), "The element should not be found in an empty array."); } + /** + * Test for exponential search with a null array. + */ + @Test + void testExponentialSearchNullArray() { + ExponentialSearch exponentialSearch = new ExponentialSearch(); + assertEquals(-1, exponentialSearch.find(null, 5), "Should return -1 for null array."); + } + + /** + * Test for exponential search with a null key. + */ + @Test + void testExponentialSearchNullKey() { + ExponentialSearch exponentialSearch = new ExponentialSearch(); + Integer[] array = {1, 2, 3, 4, 5}; + assertEquals(-1, exponentialSearch.find(array, null), "Should return -1 for null key."); + } + /** * Test for exponential search on large array. */ @@ -123,4 +154,26 @@ void testExponentialSearchNotFoundReturnsMinusOne() { assertEquals(-1, exponentialSearch.find(array, 0), "A key below the minimum should give -1."); assertEquals(-1, exponentialSearch.find(array, 12), "A key above the maximum should give -1."); } + + /** + * Test for duplicate elements in the array. + */ + @Test + void testExponentialSearchDuplicates() { + ExponentialSearch exponentialSearch = new ExponentialSearch(); + Integer[] array = {1, 2, 2, 2, 3}; + int index = exponentialSearch.find(array, 2); + assertTrue(index >= 1 && index <= 3, "Index should be one of the occurrences of 2."); + } + + /** + * Test for generic Comparable types such as Strings. + */ + @Test + void testExponentialSearchStrings() { + ExponentialSearch exponentialSearch = new ExponentialSearch(); + String[] words = {"apple", "banana", "cherry", "date", "elderberry"}; + assertEquals(2, exponentialSearch.find(words, "cherry"), "Should find 'cherry' at index 2."); + assertEquals(-1, exponentialSearch.find(words, "fig"), "Should return -1 for absent word."); + } }