From a9533d2c1470579c99857d4d4c7d75eac95da3f2 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Wed, 9 Oct 2024 12:06:21 +0530 Subject: [PATCH 1/3] Add tests, remove `main`, improve docs in `InterpolationSearch` --- .../searches/InterpolationSearch.java | 47 +++----- .../searches/InterpolationSearchTest.java | 104 ++++++++++++++++++ 2 files changed, 121 insertions(+), 30 deletions(-) create mode 100644 src/test/java/com/thealgorithms/searches/InterpolationSearchTest.java diff --git a/src/main/java/com/thealgorithms/searches/InterpolationSearch.java b/src/main/java/com/thealgorithms/searches/InterpolationSearch.java index aa1ff412b6a7..3ac6be25bf53 100644 --- a/src/main/java/com/thealgorithms/searches/InterpolationSearch.java +++ b/src/main/java/com/thealgorithms/searches/InterpolationSearch.java @@ -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. + * + *

+ * 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) + *

* *

- * 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. + *

* * @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 @@ -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); - } } diff --git a/src/test/java/com/thealgorithms/searches/InterpolationSearchTest.java b/src/test/java/com/thealgorithms/searches/InterpolationSearchTest.java new file mode 100644 index 000000000000..797e537d2cd7 --- /dev/null +++ b/src/test/java/com/thealgorithms/searches/InterpolationSearchTest.java @@ -0,0 +1,104 @@ +package com.thealgorithms.searches; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; + +import java.util.stream.IntStream; + +/** + * 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 + int expectedIndex = -1; // Key not found + assertEquals(expectedIndex, 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 + int expectedIndex = 0; // Index of the key in the array + assertEquals(expectedIndex, 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 + int expectedIndex = -1; // Key not found + assertEquals(expectedIndex, 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 + int expectedIndex = -1; // Key not found + assertEquals(expectedIndex, 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 + int expectedIndex = 4999; // Index of the last element + assertEquals(expectedIndex, 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 + int expectedIndex = 6; // Index of the key in the array + assertEquals(expectedIndex, interpolationSearch.find(array, key), + "The index of the found element should be 6."); + } +} From b1061c8fcd4d6ec89f80ae1d3362d5ec37b3ed71 Mon Sep 17 00:00:00 2001 From: Hardvan Date: Wed, 9 Oct 2024 06:36:39 +0000 Subject: [PATCH 2/3] Update directory --- DIRECTORY.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 6042dd1b5e0d..544404e8477d 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -31,6 +31,7 @@ * [IsPowerTwo](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/IsPowerTwo.java) * [LowestSetBit](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/LowestSetBit.java) * [NonRepeatingNumberFinder](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/NonRepeatingNumberFinder.java) + * [NumberAppearingOddTimes](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/NumberAppearingOddTimes.java) * [NumbersDifferentSigns](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/NumbersDifferentSigns.java) * [ReverseBits](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/ReverseBits.java) * [SingleBitOperations](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/SingleBitOperations.java) @@ -638,6 +639,7 @@ * [IsPowerTwoTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/IsPowerTwoTest.java) * [LowestSetBitTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/LowestSetBitTest.java) * [NonRepeatingNumberFinderTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/NonRepeatingNumberFinderTest.java) + * [NumberAppearingOddTimesTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/NumberAppearingOddTimesTest.java) * [NumbersDifferentSignsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/NumbersDifferentSignsTest.java) * [ReverseBitsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/ReverseBitsTest.java) * [SingleBitOperationsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/SingleBitOperationsTest.java) @@ -969,6 +971,7 @@ * [BreadthFirstSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/BreadthFirstSearchTest.java) * [DepthFirstSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/DepthFirstSearchTest.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) From 695dd726d6763f474b1ac01e1976e0f2d4c82051 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Wed, 9 Oct 2024 12:08:10 +0530 Subject: [PATCH 3/3] Fix --- .../searches/InterpolationSearchTest.java | 30 +++++-------------- 1 file changed, 8 insertions(+), 22 deletions(-) diff --git a/src/test/java/com/thealgorithms/searches/InterpolationSearchTest.java b/src/test/java/com/thealgorithms/searches/InterpolationSearchTest.java index 797e537d2cd7..b3b7e7ef129c 100644 --- a/src/test/java/com/thealgorithms/searches/InterpolationSearchTest.java +++ b/src/test/java/com/thealgorithms/searches/InterpolationSearchTest.java @@ -2,9 +2,8 @@ import static org.junit.jupiter.api.Assertions.assertEquals; -import org.junit.jupiter.api.Test; - import java.util.stream.IntStream; +import org.junit.jupiter.api.Test; /** * Unit tests for the InterpolationSearch class. @@ -20,8 +19,7 @@ void testInterpolationSearchFound() { 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."); + assertEquals(expectedIndex, interpolationSearch.find(array, key), "The index of the found element should be 7."); } /** @@ -32,9 +30,7 @@ void testInterpolationSearchNotFound() { InterpolationSearch interpolationSearch = new InterpolationSearch(); int[] array = {1, 2, 4, 8, 16}; int key = 6; // Element not present in the array - int expectedIndex = -1; // Key not found - assertEquals(expectedIndex, interpolationSearch.find(array, key), - "The element should not be found in the array."); + assertEquals(-1, interpolationSearch.find(array, key), "The element should not be found in the array."); } /** @@ -45,9 +41,7 @@ void testInterpolationSearchFirstElement() { InterpolationSearch interpolationSearch = new InterpolationSearch(); int[] array = {1, 2, 4, 8, 16}; int key = 1; // First element - int expectedIndex = 0; // Index of the key in the array - assertEquals(expectedIndex, interpolationSearch.find(array, key), - "The index of the first element should be 0."); + assertEquals(0, interpolationSearch.find(array, key), "The index of the first element should be 0."); } /** @@ -58,9 +52,7 @@ void testInterpolationSearchSingleElementNotFound() { InterpolationSearch interpolationSearch = new InterpolationSearch(); int[] array = {1}; int key = 2; // Key not present - int expectedIndex = -1; // Key not found - assertEquals(expectedIndex, interpolationSearch.find(array, key), - "The element should not be found in the array."); + assertEquals(-1, interpolationSearch.find(array, key), "The element should not be found in the array."); } /** @@ -71,9 +63,7 @@ void testInterpolationSearchEmptyArray() { InterpolationSearch interpolationSearch = new InterpolationSearch(); int[] array = {}; // Empty array int key = 1; // Key not present - int expectedIndex = -1; // Key not found - assertEquals(expectedIndex, interpolationSearch.find(array, key), - "The element should not be found in an empty array."); + assertEquals(-1, interpolationSearch.find(array, key), "The element should not be found in an empty array."); } /** @@ -84,9 +74,7 @@ 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 - int expectedIndex = 4999; // Index of the last element - assertEquals(expectedIndex, interpolationSearch.find(array, key), - "The index of the last element should be 4999."); + assertEquals(4999, interpolationSearch.find(array, key), "The index of the last element should be 4999."); } /** @@ -97,8 +85,6 @@ 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 - int expectedIndex = 6; // Index of the key in the array - assertEquals(expectedIndex, interpolationSearch.find(array, key), - "The index of the found element should be 6."); + assertEquals(6, interpolationSearch.find(array, key), "The index of the found element should be 6."); } }