Skip to content

Commit c4588ed

Browse files
HardvanChiefpatwal
authored andcommitted
Add tests, remove main, improve docs in InterpolationSearch (TheAlgorithms#5666)
1 parent 1906754 commit c4588ed

File tree

3 files changed

+108
-30
lines changed

3 files changed

+108
-30
lines changed

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,6 +1004,7 @@
10041004
* [ExponentialSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/ExponentialSearchTest.java)
10051005
* [FibonacciSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/FibonacciSearchTest.java)
10061006
* [HowManyTimesRotatedTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/HowManyTimesRotatedTest.java)
1007+
* [InterpolationSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/InterpolationSearchTest.java)
10071008
* [KMPSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/KMPSearchTest.java)
10081009
* [OrderAgnosticBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/OrderAgnosticBinarySearchTest.java)
10091010
* [PerfectBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/PerfectBinarySearchTest.java)
Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,31 @@
11
package com.thealgorithms.searches;
22

3-
import java.util.Arrays;
4-
import java.util.Random;
5-
import java.util.stream.IntStream;
6-
73
/**
8-
* Interpolation search algorithm implementation
4+
* InterpolationSearch is an algorithm that searches for a target value within a sorted array
5+
* by estimating the position based on the values at the corners of the current search range.
6+
*
7+
* <p>
8+
* The performance of this algorithm can vary:
9+
* - Worst-case performance: O(n)
10+
* - Best-case performance: O(1)
11+
* - Average performance: O(log(log(n))) if the elements are uniformly distributed; otherwise O(n)
12+
* - Worst-case space complexity: O(1)
13+
* </p>
914
*
1015
* <p>
11-
* Worst-case performance O(n) Best-case performance O(1) Average performance
12-
* O(log(log(n))) if the elements are uniformly distributed if not O(n)
13-
* Worst-case space complexity O(1)
16+
* This search algorithm requires the input array to be sorted.
17+
* </p>
1418
*
1519
* @author Podshivalov Nikita (https://github.com/nikitap492)
1620
*/
1721
class InterpolationSearch {
1822

1923
/**
20-
* @param array is a sorted array
21-
* @param key is a value what shoulb be found in the array
22-
* @return an index if the array contains the key unless -1
24+
* Finds the index of the specified key in a sorted array using interpolation search.
25+
*
26+
* @param array The sorted array to search.
27+
* @param key The value to search for.
28+
* @return The index of the key if found, otherwise -1.
2329
*/
2430
public int find(int[] array, int key) {
2531
// Find indexes of two corners
@@ -48,23 +54,4 @@ public int find(int[] array, int key) {
4854
}
4955
return -1;
5056
}
51-
52-
// Driver method
53-
public static void main(String[] args) {
54-
Random r = new Random();
55-
int size = 100;
56-
int maxElement = 100000;
57-
int[] integers = IntStream.generate(() -> r.nextInt(maxElement)).limit(size).sorted().toArray();
58-
59-
// the element that should be found
60-
int shouldBeFound = integers[r.nextInt(size - 1)];
61-
62-
InterpolationSearch search = new InterpolationSearch();
63-
int atIndex = search.find(integers, shouldBeFound);
64-
65-
System.out.printf("Should be found: %d. Found %d at index %d. An array length %d%n", shouldBeFound, integers[atIndex], atIndex, size);
66-
67-
int toCheck = Arrays.binarySearch(integers, shouldBeFound);
68-
System.out.printf("Found by system method at an index: %d. Is equal: %b%n", toCheck, toCheck == atIndex);
69-
}
7057
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package com.thealgorithms.searches;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.util.stream.IntStream;
6+
import org.junit.jupiter.api.Test;
7+
8+
/**
9+
* Unit tests for the InterpolationSearch class.
10+
*/
11+
class InterpolationSearchTest {
12+
13+
/**
14+
* Test for basic interpolation search functionality when the element is found.
15+
*/
16+
@Test
17+
void testInterpolationSearchFound() {
18+
InterpolationSearch interpolationSearch = new InterpolationSearch();
19+
int[] array = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512};
20+
int key = 128;
21+
int expectedIndex = 7; // Index of the key in the array
22+
assertEquals(expectedIndex, interpolationSearch.find(array, key), "The index of the found element should be 7.");
23+
}
24+
25+
/**
26+
* Test for interpolation search when the element is not present in the array.
27+
*/
28+
@Test
29+
void testInterpolationSearchNotFound() {
30+
InterpolationSearch interpolationSearch = new InterpolationSearch();
31+
int[] array = {1, 2, 4, 8, 16};
32+
int key = 6; // Element not present in the array
33+
assertEquals(-1, interpolationSearch.find(array, key), "The element should not be found in the array.");
34+
}
35+
36+
/**
37+
* Test for interpolation search with the first element as the key.
38+
*/
39+
@Test
40+
void testInterpolationSearchFirstElement() {
41+
InterpolationSearch interpolationSearch = new InterpolationSearch();
42+
int[] array = {1, 2, 4, 8, 16};
43+
int key = 1; // First element
44+
assertEquals(0, interpolationSearch.find(array, key), "The index of the first element should be 0.");
45+
}
46+
47+
/**
48+
* Test for interpolation search with a single element not present.
49+
*/
50+
@Test
51+
void testInterpolationSearchSingleElementNotFound() {
52+
InterpolationSearch interpolationSearch = new InterpolationSearch();
53+
int[] array = {1};
54+
int key = 2; // Key not present
55+
assertEquals(-1, interpolationSearch.find(array, key), "The element should not be found in the array.");
56+
}
57+
58+
/**
59+
* Test for interpolation search with an empty array.
60+
*/
61+
@Test
62+
void testInterpolationSearchEmptyArray() {
63+
InterpolationSearch interpolationSearch = new InterpolationSearch();
64+
int[] array = {}; // Empty array
65+
int key = 1; // Key not present
66+
assertEquals(-1, interpolationSearch.find(array, key), "The element should not be found in an empty array.");
67+
}
68+
69+
/**
70+
* Test for interpolation search on large uniformly distributed array.
71+
*/
72+
@Test
73+
void testInterpolationSearchLargeUniformArray() {
74+
InterpolationSearch interpolationSearch = new InterpolationSearch();
75+
int[] array = IntStream.range(0, 10000).map(i -> i * 2).toArray(); // Array from 0 to 19998, step 2
76+
int key = 9998; // Last even number in the array
77+
assertEquals(4999, interpolationSearch.find(array, key), "The index of the last element should be 4999.");
78+
}
79+
80+
/**
81+
* Test for interpolation search on large non-uniformly distributed array.
82+
*/
83+
@Test
84+
void testInterpolationSearchLargeNonUniformArray() {
85+
InterpolationSearch interpolationSearch = new InterpolationSearch();
86+
int[] array = {1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144}; // Fibonacci numbers
87+
int key = 21; // Present in the array
88+
assertEquals(6, interpolationSearch.find(array, key), "The index of the found element should be 6.");
89+
}
90+
}

0 commit comments

Comments
 (0)