Skip to content

Commit a9533d2

Browse files
committed
Add tests, remove main, improve docs in InterpolationSearch
1 parent b54cc21 commit a9533d2

File tree

2 files changed

+121
-30
lines changed

2 files changed

+121
-30
lines changed
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: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package com.thealgorithms.searches;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
import java.util.stream.IntStream;
8+
9+
/**
10+
* Unit tests for the InterpolationSearch class.
11+
*/
12+
class InterpolationSearchTest {
13+
14+
/**
15+
* Test for basic interpolation search functionality when the element is found.
16+
*/
17+
@Test
18+
void testInterpolationSearchFound() {
19+
InterpolationSearch interpolationSearch = new InterpolationSearch();
20+
int[] array = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512};
21+
int key = 128;
22+
int expectedIndex = 7; // Index of the key in the array
23+
assertEquals(expectedIndex, interpolationSearch.find(array, key),
24+
"The index of the found element should be 7.");
25+
}
26+
27+
/**
28+
* Test for interpolation search when the element is not present in the array.
29+
*/
30+
@Test
31+
void testInterpolationSearchNotFound() {
32+
InterpolationSearch interpolationSearch = new InterpolationSearch();
33+
int[] array = {1, 2, 4, 8, 16};
34+
int key = 6; // Element not present in the array
35+
int expectedIndex = -1; // Key not found
36+
assertEquals(expectedIndex, interpolationSearch.find(array, key),
37+
"The element should not be found in the array.");
38+
}
39+
40+
/**
41+
* Test for interpolation search with the first element as the key.
42+
*/
43+
@Test
44+
void testInterpolationSearchFirstElement() {
45+
InterpolationSearch interpolationSearch = new InterpolationSearch();
46+
int[] array = {1, 2, 4, 8, 16};
47+
int key = 1; // First element
48+
int expectedIndex = 0; // Index of the key in the array
49+
assertEquals(expectedIndex, interpolationSearch.find(array, key),
50+
"The index of the first element should be 0.");
51+
}
52+
53+
/**
54+
* Test for interpolation search with a single element not present.
55+
*/
56+
@Test
57+
void testInterpolationSearchSingleElementNotFound() {
58+
InterpolationSearch interpolationSearch = new InterpolationSearch();
59+
int[] array = {1};
60+
int key = 2; // Key not present
61+
int expectedIndex = -1; // Key not found
62+
assertEquals(expectedIndex, interpolationSearch.find(array, key),
63+
"The element should not be found in the array.");
64+
}
65+
66+
/**
67+
* Test for interpolation search with an empty array.
68+
*/
69+
@Test
70+
void testInterpolationSearchEmptyArray() {
71+
InterpolationSearch interpolationSearch = new InterpolationSearch();
72+
int[] array = {}; // Empty array
73+
int key = 1; // Key not present
74+
int expectedIndex = -1; // Key not found
75+
assertEquals(expectedIndex, interpolationSearch.find(array, key),
76+
"The element should not be found in an empty array.");
77+
}
78+
79+
/**
80+
* Test for interpolation search on large uniformly distributed array.
81+
*/
82+
@Test
83+
void testInterpolationSearchLargeUniformArray() {
84+
InterpolationSearch interpolationSearch = new InterpolationSearch();
85+
int[] array = IntStream.range(0, 10000).map(i -> i * 2).toArray(); // Array from 0 to 19998, step 2
86+
int key = 9998; // Last even number in the array
87+
int expectedIndex = 4999; // Index of the last element
88+
assertEquals(expectedIndex, interpolationSearch.find(array, key),
89+
"The index of the last element should be 4999.");
90+
}
91+
92+
/**
93+
* Test for interpolation search on large non-uniformly distributed array.
94+
*/
95+
@Test
96+
void testInterpolationSearchLargeNonUniformArray() {
97+
InterpolationSearch interpolationSearch = new InterpolationSearch();
98+
int[] array = {1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144}; // Fibonacci numbers
99+
int key = 21; // Present in the array
100+
int expectedIndex = 6; // Index of the key in the array
101+
assertEquals(expectedIndex, interpolationSearch.find(array, key),
102+
"The index of the found element should be 6.");
103+
}
104+
}

0 commit comments

Comments
 (0)