Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* Note: This algorithm requires that the input array be sorted.
* </p>
*/
class ExponentialSearch implements SearchAlgorithm {
public final class ExponentialSearch implements SearchAlgorithm {

/**
* Finds the index of the specified key in a sorted array using exponential search.
Expand All @@ -31,13 +31,13 @@ class ExponentialSearch implements SearchAlgorithm {
*/
@Override
public <T extends Comparable<T>> 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;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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.
*/
Expand All @@ -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.
*/
Expand Down Expand Up @@ -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.");
}
}
Loading