mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-05 16:27:33 +08:00
Add tests, remove main
, improve docs in InterpolationSearch
(#5666)
This commit is contained in:
@ -1004,6 +1004,7 @@
|
|||||||
* [ExponentialSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/ExponentialSearchTest.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)
|
||||||
|
* [InterpolationSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/InterpolationSearchTest.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)
|
||||||
* [OrderAgnosticBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/OrderAgnosticBinarySearchTest.java)
|
* [OrderAgnosticBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/OrderAgnosticBinarySearchTest.java)
|
||||||
* [PerfectBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/PerfectBinarySearchTest.java)
|
* [PerfectBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/PerfectBinarySearchTest.java)
|
||||||
|
@ -1,25 +1,31 @@
|
|||||||
package com.thealgorithms.searches;
|
package com.thealgorithms.searches;
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.Random;
|
|
||||||
import java.util.stream.IntStream;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Interpolation search algorithm implementation
|
* InterpolationSearch is an algorithm that searches for a target value within a sorted array
|
||||||
|
* by estimating the position based on the values at the corners of the current search range.
|
||||||
*
|
*
|
||||||
* <p>
|
* <p>
|
||||||
* Worst-case performance O(n) Best-case performance O(1) Average performance
|
* The performance of this algorithm can vary:
|
||||||
* O(log(log(n))) if the elements are uniformly distributed if not O(n)
|
* - Worst-case performance: O(n)
|
||||||
* Worst-case space complexity O(1)
|
* - Best-case performance: O(1)
|
||||||
|
* - Average performance: O(log(log(n))) if the elements are uniformly distributed; otherwise O(n)
|
||||||
|
* - Worst-case space complexity: O(1)
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* <p>
|
||||||
|
* This search algorithm requires the input array to be sorted.
|
||||||
|
* </p>
|
||||||
*
|
*
|
||||||
* @author Podshivalov Nikita (https://github.com/nikitap492)
|
* @author Podshivalov Nikita (https://github.com/nikitap492)
|
||||||
*/
|
*/
|
||||||
class InterpolationSearch {
|
class InterpolationSearch {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array is a sorted array
|
* Finds the index of the specified key in a sorted array using interpolation search.
|
||||||
* @param key is a value what shoulb be found in the array
|
*
|
||||||
* @return an index if the array contains the key unless -1
|
* @param array The sorted array to search.
|
||||||
|
* @param key The value to search for.
|
||||||
|
* @return The index of the key if found, otherwise -1.
|
||||||
*/
|
*/
|
||||||
public int find(int[] array, int key) {
|
public int find(int[] array, int key) {
|
||||||
// Find indexes of two corners
|
// Find indexes of two corners
|
||||||
@ -48,23 +54,4 @@ class InterpolationSearch {
|
|||||||
}
|
}
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Driver method
|
|
||||||
public static void main(String[] args) {
|
|
||||||
Random r = new Random();
|
|
||||||
int size = 100;
|
|
||||||
int maxElement = 100000;
|
|
||||||
int[] integers = IntStream.generate(() -> r.nextInt(maxElement)).limit(size).sorted().toArray();
|
|
||||||
|
|
||||||
// the element that should be found
|
|
||||||
int shouldBeFound = integers[r.nextInt(size - 1)];
|
|
||||||
|
|
||||||
InterpolationSearch search = new InterpolationSearch();
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,90 @@
|
|||||||
|
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 InterpolationSearch class.
|
||||||
|
*/
|
||||||
|
class InterpolationSearchTest {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test for basic interpolation search functionality when the element is found.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
void testInterpolationSearchFound() {
|
||||||
|
InterpolationSearch interpolationSearch = new InterpolationSearch();
|
||||||
|
int[] array = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512};
|
||||||
|
int key = 128;
|
||||||
|
int expectedIndex = 7; // Index of the key in the array
|
||||||
|
assertEquals(expectedIndex, interpolationSearch.find(array, key), "The index of the found element should be 7.");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test for interpolation search when the element is not present in the array.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
void testInterpolationSearchNotFound() {
|
||||||
|
InterpolationSearch interpolationSearch = new InterpolationSearch();
|
||||||
|
int[] array = {1, 2, 4, 8, 16};
|
||||||
|
int key = 6; // Element not present in the array
|
||||||
|
assertEquals(-1, interpolationSearch.find(array, key), "The element should not be found in the array.");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test for interpolation search with the first element as the key.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
void testInterpolationSearchFirstElement() {
|
||||||
|
InterpolationSearch interpolationSearch = new InterpolationSearch();
|
||||||
|
int[] array = {1, 2, 4, 8, 16};
|
||||||
|
int key = 1; // First element
|
||||||
|
assertEquals(0, interpolationSearch.find(array, key), "The index of the first element should be 0.");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test for interpolation search with a single element not present.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
void testInterpolationSearchSingleElementNotFound() {
|
||||||
|
InterpolationSearch interpolationSearch = new InterpolationSearch();
|
||||||
|
int[] array = {1};
|
||||||
|
int key = 2; // Key not present
|
||||||
|
assertEquals(-1, interpolationSearch.find(array, key), "The element should not be found in the array.");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test for interpolation search with an empty array.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
void testInterpolationSearchEmptyArray() {
|
||||||
|
InterpolationSearch interpolationSearch = new InterpolationSearch();
|
||||||
|
int[] array = {}; // Empty array
|
||||||
|
int key = 1; // Key not present
|
||||||
|
assertEquals(-1, interpolationSearch.find(array, key), "The element should not be found in an empty array.");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test for interpolation search on large uniformly distributed array.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
void testInterpolationSearchLargeUniformArray() {
|
||||||
|
InterpolationSearch interpolationSearch = new InterpolationSearch();
|
||||||
|
int[] array = IntStream.range(0, 10000).map(i -> i * 2).toArray(); // Array from 0 to 19998, step 2
|
||||||
|
int key = 9998; // Last even number in the array
|
||||||
|
assertEquals(4999, interpolationSearch.find(array, key), "The index of the last element should be 4999.");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test for interpolation search on large non-uniformly distributed array.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
void testInterpolationSearchLargeNonUniformArray() {
|
||||||
|
InterpolationSearch interpolationSearch = new InterpolationSearch();
|
||||||
|
int[] array = {1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144}; // Fibonacci numbers
|
||||||
|
int key = 21; // Present in the array
|
||||||
|
assertEquals(6, interpolationSearch.find(array, key), "The index of the found element should be 6.");
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user