Repository: TheAlgorithms/Java
Pull request #5669 (2024-10-09 06:49:24)

Add tests, remove main in JumpSearch
<!--
Thank you for your contribution!
In order to reduce the number of notifications sent to the maintainers, please:
- create your PR as draft, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests#draft-pull-requests,
- make sure that all of the CI checks pass,
- mark your PR as ready for review, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-stage-of-a-pull-request#marking-a-pull-request-as-ready-for-review
-->

<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
- [x] All new code is formatted with `clang-format -i --style=file path/to/your/file.java`

codecov-commenter: ## [Codecov](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5669?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 58.96%. Comparing base [(`b54cc21`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/b54cc21ade112e20f429964dc86f8543a5605d98?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`bea2a3b`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/bea2a3b0b4dc1efb026b8e5cc2ecf879bdd5f17b?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).
> Report is 3 commits behind head on master.

<details><summary>Additional details and impacted files</summary>


```diff
@@             Coverage Diff              @@
##             master    #5669      +/-   ##
============================================
+ Coverage     58.59%   58.96%   +0.36%     
- Complexity     3749     3776      +27     
============================================
  Files           554      556       +2     
  Lines         15922    15944      +22     
  Branches       3030     3036       +6     
============================================
+ Hits           9330     9401      +71     
+ Misses         6195     6139      -56     
- Partials        397      404       +7     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5669?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).


Patch:
diff --git a/DIRECTORY.md b/DIRECTORY.md
index ff6e2980..a22863e8 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 f499cf80..8dcec3a8 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.
+ *
+ * <p>
+ * 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.
+ *
+ * <p>
+ * 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.
+ *
+ * <p>
+ * Worst-case performance: O(√N)<br>
+ * Best-case performance: O(1)<br>
+ * Average performance: O(√N)<br>
+ * Worst-case space complexity: O(1)
+ *
+ * <p>
+ * 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 <tt>-1</tt>
+     * @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 <T extends Comparable<T>> int find(T[] array, T key) {
         int length = array.length;
-        /* length of array */
         int blockSize = (int) Math.sqrt(length);
-        /* block size to be jumped */
 
         int limit = blockSize;
-        while (key.compareTo(array[limit]) > 0 && limit < array.length - 1) {
-            limit = Math.min(limit + blockSize, array.length - 1);
+        // Jumping ahead to find the block where the key may be located
+        while (limit < length && key.compareTo(array[limit]) > 0) {
+            limit = Math.min(limit + blockSize, length - 1);
         }
 
-        for (int i = limit - blockSize; i <= limit; i++) {
-            if (array[i] == key) {
-                /* execute linear search */
+        // Perform linear search within the identified block
+        for (int i = limit - blockSize; i <= limit && i < length; i++) {
+            if (array[i].equals(key)) {
                 return i;
             }
         }
         return -1;
-        /* not found */
     }
 }


Test patch:
diff --git a/src/test/java/com/thealgorithms/searches/JumpSearchTest.java b/src/test/java/com/thealgorithms/searches/JumpSearchTest.java
new file mode 100644
index 00000000..3fa319b6
--- /dev/null
+++ b/src/test/java/com/thealgorithms/searches/JumpSearchTest.java
@@ -0,0 +1,94 @@
+package com.thealgorithms.searches;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import org.junit.jupiter.api.Test;
+
+/**
+ * Unit tests for the JumpSearch class.
+ */
+class JumpSearchTest {
+
+    /**
+     * Test for finding an element present in the array.
+     */
+    @Test
+    void testJumpSearchFound() {
+        JumpSearch jumpSearch = new JumpSearch();
+        Integer[] array = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
+        Integer key = 5; // Element to find
+        assertEquals(5, jumpSearch.find(array, key), "The index of the found element should be 5.");
+    }
+
+    /**
+     * Test for finding the first element in the array.
+     */
+    @Test
+    void testJumpSearchFirstElement() {
+        JumpSearch jumpSearch = new JumpSearch();
+        Integer[] array = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
+        Integer key = 0; // First element
+        assertEquals(0, jumpSearch.find(array, key), "The index of the first element should be 0.");
+    }
+
+    /**
+     * Test for finding the last element in the array.
+     */
+    @Test
+    void testJumpSearchLastElement() {
+        JumpSearch jumpSearch = new JumpSearch();
+        Integer[] array = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
+        Integer key = 10; // Last element
+        assertEquals(10, jumpSearch.find(array, key), "The index of the last element should be 10.");
+    }
+
+    /**
+     * Test for finding an element not present in the array.
+     */
+    @Test
+    void testJumpSearchNotFound() {
+        JumpSearch jumpSearch = new JumpSearch();
+        Integer[] array = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
+        Integer key = -1; // Element not in the array
+        assertEquals(-1, jumpSearch.find(array, key), "The element should not be found in the array.");
+    }
+
+    /**
+     * Test for finding an element in an empty array.
+     */
+    @Test
+    void testJumpSearchEmptyArray() {
+        JumpSearch jumpSearch = new JumpSearch();
+        Integer[] array = {}; // Empty array
+        Integer key = 1; // Key not present
+        assertEquals(-1, jumpSearch.find(array, key), "The element should not be found in an empty array.");
+    }
+
+    /**
+     * Test for finding an element in a large array.
+     */
+    @Test
+    void testJumpSearchLargeArray() {
+        JumpSearch jumpSearch = new JumpSearch();
+        Integer[] array = new Integer[1000];
+        for (int i = 0; i < array.length; i++) {
+            array[i] = i * 2; // Fill the array with even numbers
+        }
+        Integer key = 256; // Present in the array
+        assertEquals(128, jumpSearch.find(array, key), "The index of the found element should be 128.");
+    }
+
+    /**
+     * Test for finding an element in a large array when it is not present.
+     */
+    @Test
+    void testJumpSearchLargeArrayNotFound() {
+        JumpSearch jumpSearch = new JumpSearch();
+        Integer[] array = new Integer[1000];
+        for (int i = 0; i < array.length; i++) {
+            array[i] = i * 2; // Fill the array with even numbers
+        }
+        Integer key = 999; // Key not present
+        assertEquals(-1, jumpSearch.find(array, key), "The element should not be found in the array.");
+    }
+}