mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-05 16:27:33 +08:00
Add tests, remove main
, improve docs in ExponentialSearch
(#5664)
This commit is contained in:
@ -1001,6 +1001,7 @@
|
|||||||
* [BM25InvertedIndexTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/BM25InvertedIndexTest.java)
|
* [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)
|
* [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)
|
* [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)
|
||||||
* [FibonacciSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/FibonacciSearchTest.java)
|
* [FibonacciSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/FibonacciSearchTest.java)
|
||||||
* [HowManyTimesRotatedTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/HowManyTimesRotatedTest.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)
|
* [KMPSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/KMPSearchTest.java)
|
||||||
|
@ -2,44 +2,47 @@ package com.thealgorithms.searches;
|
|||||||
|
|
||||||
import com.thealgorithms.devutils.searches.SearchAlgorithm;
|
import com.thealgorithms.devutils.searches.SearchAlgorithm;
|
||||||
import java.util.Arrays;
|
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 {
|
class ExponentialSearch implements SearchAlgorithm {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
/**
|
||||||
Random r = ThreadLocalRandom.current();
|
* Finds the index of the specified key in a sorted array using exponential search.
|
||||||
|
*
|
||||||
int size = 100;
|
* @param array The sorted array to search.
|
||||||
int maxElement = 100000;
|
* @param key The element to search for.
|
||||||
|
* @param <T> The type of the elements in the array, which must be comparable.
|
||||||
Integer[] integers = IntStream.generate(() -> r.nextInt(maxElement)).limit(size).sorted().boxed().toArray(Integer[] ::new);
|
* @return The index of the key if found, otherwise -1.
|
||||||
|
*/
|
||||||
// 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);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <T extends Comparable<T>> int find(T[] array, T key) {
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
if (array[array.length - 1] == key) {
|
if (array[array.length - 1].equals(key)) {
|
||||||
return array.length;
|
return array.length - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int range = 1;
|
int range = 1;
|
||||||
|
while (range < array.length && array[range].compareTo(key) < 0) {
|
||||||
while (range < array.length && array[range].compareTo(key) <= -1) {
|
|
||||||
range = range * 2;
|
range = range * 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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.");
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user