From 38285771c8bf1e4bf1c3741d4ce397d744b01136 Mon Sep 17 00:00:00 2001 From: Hardik Pawar <97388607+Hardvan@users.noreply.github.com> Date: Fri, 11 Oct 2024 01:21:06 +0530 Subject: [PATCH] Add tests, remove main in JumpSearch (#5669) --- DIRECTORY.md | 1 + .../thealgorithms/searches/JumpSearch.java | 55 ++++++----- .../searches/JumpSearchTest.java | 94 +++++++++++++++++++ 3 files changed, 128 insertions(+), 22 deletions(-) create mode 100644 src/test/java/com/thealgorithms/searches/JumpSearchTest.java diff --git a/DIRECTORY.md b/DIRECTORY.md index ff6e29806..a22863e8b 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -1007,6 +1007,7 @@ * [InterpolationSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/InterpolationSearchTest.java) * [IterativeBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/IterativeBinarySearchTest.java) * [IterativeTernarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/IterativeTernarySearchTest.java) + * [JumpSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/JumpSearchTest.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) diff --git a/src/main/java/com/thealgorithms/searches/JumpSearch.java b/src/main/java/com/thealgorithms/searches/JumpSearch.java index f499cf807..8dcec3a81 100644 --- a/src/main/java/com/thealgorithms/searches/JumpSearch.java +++ b/src/main/java/com/thealgorithms/searches/JumpSearch.java @@ -2,44 +2,55 @@ package com.thealgorithms.searches; import com.thealgorithms.devutils.searches.SearchAlgorithm; +/** + * An implementation of the Jump Search algorithm. + * + *
+ * Jump Search is an algorithm for searching sorted arrays. It works by dividing the array + * into blocks of a fixed size (the block size is typically the square root of the array length) + * and jumping ahead by this block size to find a range where the target element may be located. + * Once the range is found, a linear search is performed within that block. + * + *
+ * The Jump Search algorithm is particularly effective for large sorted arrays where the cost of + * performing a linear search on the entire array would be prohibitive. + * + *
+ * Worst-case performance: O(√N)
+ * Best-case performance: O(1)
+ * Average performance: O(√N)
+ * Worst-case space complexity: O(1)
+ *
+ *
+ * This class implements the {@link SearchAlgorithm} interface, providing a generic search method
+ * for any comparable type.
+ */
public class JumpSearch implements SearchAlgorithm {
- public static void main(String[] args) {
- JumpSearch jumpSearch = new JumpSearch();
- Integer[] array = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
- for (int i = 0; i < array.length; i++) {
- assert jumpSearch.find(array, i) == i;
- }
- assert jumpSearch.find(array, -1) == -1;
- assert jumpSearch.find(array, 11) == -1;
- }
-
/**
- * Jump Search algorithm implements
+ * Jump Search algorithm implementation.
*
- * @param array the array contains elements
- * @param key to be searched
- * @return index of {@code key} if found, otherwise -1
+ * @param array the sorted array containing elements
+ * @param key the element to be searched
+ * @return the index of {@code key} if found, otherwise -1
*/
@Override
public