mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-06 09:06:51 +08:00
Add tests, remove main
, improve docs in ExponentialSearch
(#5664)
This commit is contained in:
@ -2,44 +2,47 @@ package com.thealgorithms.searches;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user