Skip to content

Add tests, remove main, improve docs in ExponentialSearch #5664

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from 16 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
3 changes: 3 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
* [IsPowerTwo](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/IsPowerTwo.java)
* [LowestSetBit](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/LowestSetBit.java)
* [NonRepeatingNumberFinder](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/NonRepeatingNumberFinder.java)
* [NumberAppearingOddTimes](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/NumberAppearingOddTimes.java)
* [NumbersDifferentSigns](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/NumbersDifferentSigns.java)
* [ReverseBits](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/ReverseBits.java)
* [SingleBitOperations](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/SingleBitOperations.java)
Expand Down Expand Up @@ -638,6 +639,7 @@
* [IsPowerTwoTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/IsPowerTwoTest.java)
* [LowestSetBitTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/LowestSetBitTest.java)
* [NonRepeatingNumberFinderTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/NonRepeatingNumberFinderTest.java)
* [NumberAppearingOddTimesTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/NumberAppearingOddTimesTest.java)
* [NumbersDifferentSignsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/NumbersDifferentSignsTest.java)
* [ReverseBitsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/ReverseBitsTest.java)
* [SingleBitOperationsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/SingleBitOperationsTest.java)
Expand Down Expand Up @@ -968,6 +970,7 @@
* [BM25InvertedIndexTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/BM25InvertedIndexTest.java)
* [BreadthFirstSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/BreadthFirstSearchTest.java)
* [DepthFirstSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/DepthFirstSearchTest.java)
* [ExponentialSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/ExponentialSearchTest.java)
* [HowManyTimesRotatedTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/HowManyTimesRotatedTest.java)
* [KMPSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/KMPSearchTest.java)
* [OrderAgnosticBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/OrderAgnosticBinarySearchTest.java)
Expand Down
59 changes: 31 additions & 28 deletions src/main/java/com/thealgorithms/searches/ExponentalSearch.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,47 @@

import com.thealgorithms.devutils.searches.SearchAlgorithm;
import java.util.Arrays;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.IntStream;

/**
* ExponentialSearch is an algorithm that efficiently finds the position of a target
* value within a sorted array. It works by expanding the range to find the bounds
* where the target might exist and then using binary search within that range.
*
* <p>
* Worst-case time complexity: O(log n)
* Best-case time complexity: O(1) when the element is found at the first position.
* Average time complexity: O(log n)
* Worst-case space complexity: O(1)
* </p>
*
* <p>
* Note: This algorithm requires that the input array be sorted.
* </p>
*/
class ExponentialSearch implements SearchAlgorithm {

public static void main(String[] args) {
Random r = ThreadLocalRandom.current();

int size = 100;
int maxElement = 100000;

Integer[] integers = IntStream.generate(() -> r.nextInt(maxElement)).limit(size).sorted().boxed().toArray(Integer[] ::new);

// The element that should be found
int shouldBeFound = integers[r.nextInt(size - 1)];

ExponentialSearch search = new ExponentialSearch();
int atIndex = search.find(integers, shouldBeFound);

System.out.printf("Should be found: %d. Found %d at index %d. An array length %d%n", shouldBeFound, integers[atIndex], atIndex, size);

int toCheck = Arrays.binarySearch(integers, shouldBeFound);
System.out.printf("Found by system method at an index: %d. Is equal: %b%n", toCheck, toCheck == atIndex);
}

/**
* Finds the index of the specified key in a sorted array using exponential search.
*
* @param array The sorted array to search.
* @param key The element to search for.
* @param <T> The type of the elements in the array, which must be comparable.
* @return The index of the key if found, otherwise -1.
*/
@Override
public <T extends Comparable<T>> int find(T[] array, T key) {
if (array[0] == key) {
if (array.length == 0) {
return -1;
}
if (array[0].equals(key)) {
return 0;
}
if (array[array.length - 1] == key) {
return array.length;
if (array[array.length - 1].equals(key)) {
return array.length - 1;
}

int range = 1;

while (range < array.length && array[range].compareTo(key) <= -1) {
while (range < array.length && array[range].compareTo(key) < 0) {
range = range * 2;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.thealgorithms.searches;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.stream.IntStream;
import org.junit.jupiter.api.Test;

/**
* Unit tests for the ExponentialSearch class.
*/
class ExponentialSearchTest {

/**
* Test for basic exponential search functionality.
*/
@Test
void testExponentialSearchFound() {
ExponentialSearch exponentialSearch = new ExponentialSearch();
Integer[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int key = 7;
int expectedIndex = 6; // Index of the key in the array
assertEquals(expectedIndex, exponentialSearch.find(array, key), "The index of the found element should be 6.");
}

/**
* Test for exponential search with the first element as the key.
*/
@Test
void testExponentialSearchFirstElement() {
ExponentialSearch exponentialSearch = new ExponentialSearch();
Integer[] array = {1, 2, 3, 4, 5};
int key = 1; // First element
int expectedIndex = 0; // Index of the key in the array
assertEquals(expectedIndex, exponentialSearch.find(array, key), "The index of the first element should be 0.");
}

/**
* Test for exponential search with the last element as the key.
*/
@Test
void testExponentialSearchLastElement() {
ExponentialSearch exponentialSearch = new ExponentialSearch();
Integer[] array = {1, 2, 3, 4, 5};
int key = 5; // Last element
int expectedIndex = 4; // Index of the key in the array
assertEquals(expectedIndex, exponentialSearch.find(array, key), "The index of the last element should be 4.");
}

/**
* Test for exponential search with a single element present.
*/
@Test
void testExponentialSearchSingleElementFound() {
ExponentialSearch exponentialSearch = new ExponentialSearch();
Integer[] array = {1};
int key = 1; // Only element present
int expectedIndex = 0; // Index of the key in the array
assertEquals(expectedIndex, exponentialSearch.find(array, key), "The index of the single element should be 0.");
}

/**
* Test for exponential search with an empty array.
*/
@Test
void testExponentialSearchEmptyArray() {
ExponentialSearch exponentialSearch = new ExponentialSearch();
Integer[] array = {}; // Empty array
int key = 1; // Key not present
int expectedIndex = -1; // Key not found
assertEquals(expectedIndex, exponentialSearch.find(array, key), "The element should not be found in an empty array.");
}

/**
* Test for exponential search on large array.
*/
@Test
void testExponentialSearchLargeArray() {
ExponentialSearch exponentialSearch = new ExponentialSearch();
Integer[] array = IntStream.range(0, 10000).boxed().toArray(Integer[] ::new); // Array from 0 to 9999
int key = 9999;
int expectedIndex = 9999;
assertEquals(expectedIndex, exponentialSearch.find(array, key), "The index of the last element should be 9999.");
}
}