Skip to content

Add tests, remove main, improve docs in InterpolationSearch #5666

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -1004,6 +1004,7 @@
* [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)
* [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)
* [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)
Expand Down
47 changes: 17 additions & 30 deletions src/main/java/com/thealgorithms/searches/InterpolationSearch.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
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>
* The performance of this algorithm can vary:
* - Worst-case performance: O(n)
* - 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>
* Worst-case performance O(n) Best-case performance O(1) Average performance
* O(log(log(n))) if the elements are uniformly distributed if not O(n)
* Worst-case space complexity O(1)
* This search algorithm requires the input array to be sorted.
* </p>
*
* @author Podshivalov Nikita (https://github.com/nikitap492)
*/
class InterpolationSearch {

/**
* @param array is a sorted array
* @param key is a value what shoulb be found in the array
* @return an index if the array contains the key unless -1
* Finds the index of the specified key in a sorted array using interpolation search.
*
* @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) {
// Find indexes of two corners
Expand Down Expand Up @@ -48,23 +54,4 @@ public int find(int[] array, int key) {
}
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);
}
}
Original file line number Diff line number Diff line change
@@ -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.");
}
}