From 89a050ff9cd256a81e795f8b79bbc7df7c18480d Mon Sep 17 00:00:00 2001 From: Fahham29 Date: Fri, 18 Jul 2025 10:30:45 +0530 Subject: [PATCH 01/11] Add Jump Search algorithm in Java --- src/search/JumpSearch.java | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 src/search/JumpSearch.java diff --git a/src/search/JumpSearch.java b/src/search/JumpSearch.java new file mode 100644 index 000000000000..2d27a8d1a21f --- /dev/null +++ b/src/search/JumpSearch.java @@ -0,0 +1,31 @@ +package search; + +public class JumpSearch { + + public static int jumpSearch(int[] arr, int target) { + int n = arr.length; + int step = (int) Math.floor(Math.sqrt(n)); + int prev = 0; + + while (arr[Math.min(step, n)-1] < target) { + prev = step; + step += Math.floor(Math.sqrt(n)); + if (prev >= n) + return -1; + } + + for (int i = prev; i < Math.min(step, n); i++) { + if (arr[i] == target) + return i; + } + + return -1; + } + + public static void main(String[] args) { + int[] arr = {1, 3, 5, 7, 9, 12, 17, 21, 25}; + int target = 12; + int index = jumpSearch(arr, target); + System.out.println("Found at index: " + index); + } +} From 5d9eb003a8a77d764409dcb8b7915fe3dbf54fe9 Mon Sep 17 00:00:00 2001 From: Fahham29 Date: Fri, 18 Jul 2025 10:55:56 +0530 Subject: [PATCH 02/11] Fix formatting, structure, and docstring for Jump Search --- src/{search => searching}/JumpSearch.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) rename src/{search => searching}/JumpSearch.java (80%) diff --git a/src/search/JumpSearch.java b/src/searching/JumpSearch.java similarity index 80% rename from src/search/JumpSearch.java rename to src/searching/JumpSearch.java index 2d27a8d1a21f..4e797c3c2c47 100644 --- a/src/search/JumpSearch.java +++ b/src/searching/JumpSearch.java @@ -1,4 +1,13 @@ -package search; +package searching; + +/** + * Implementation of Jump Search algorithm. + * + * Time Complexity: O(√n) + * Space Complexity: O(1) + * + * Reference: https://en.wikipedia.org/wiki/Jump_search + */ public class JumpSearch { From ba7cb8f4dbefc31ea56797c8ff8d78835516b9ac Mon Sep 17 00:00:00 2001 From: Fahham29 Date: Fri, 18 Jul 2025 11:04:36 +0530 Subject: [PATCH 03/11] Fix structure and package for Jump Search algorithm --- .../thealgorithms/searches/JumpSearch.java | 66 +++++++------------ src/searching/JumpSearch.java | 40 ----------- 2 files changed, 25 insertions(+), 81 deletions(-) delete mode 100644 src/searching/JumpSearch.java diff --git a/src/main/java/com/thealgorithms/searches/JumpSearch.java b/src/main/java/com/thealgorithms/searches/JumpSearch.java index 8dcec3a819a4..a599d5baa27e 100644 --- a/src/main/java/com/thealgorithms/searches/JumpSearch.java +++ b/src/main/java/com/thealgorithms/searches/JumpSearch.java @@ -1,56 +1,40 @@ 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. + * Implementation of Jump Search algorithm. * - *

- * 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. + * Time Complexity: O(√n) + * Space Complexity: O(1) * - *

- * 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. + * Reference: https://en.wikipedia.org/wiki/Jump_search */ -public class JumpSearch implements SearchAlgorithm { - /** - * Jump Search algorithm implementation. - * - * @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 > int find(T[] array, T key) { - int length = array.length; - int blockSize = (int) Math.sqrt(length); +public class JumpSearch { + + public static int jumpSearch(int[] arr, int target) { + int n = arr.length; + int step = (int) Math.floor(Math.sqrt(n)); + int prev = 0; - int limit = blockSize; - // 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); + while (arr[Math.min(step, n) - 1] < target) { + prev = step; + step += Math.floor(Math.sqrt(n)); + if (prev >= n) + return -1; } - // Perform linear search within the identified block - for (int i = limit - blockSize; i <= limit && i < length; i++) { - if (array[i].equals(key)) { + for (int i = prev; i < Math.min(step, n); i++) { + if (arr[i] == target) return i; - } } + return -1; } + + public static void main(String[] args) { + int[] arr = { 1, 3, 5, 7, 9, 12, 17, 21, 25 }; + int target = 12; + int index = jumpSearch(arr, target); + System.out.println("Found at index: " + index); + } } diff --git a/src/searching/JumpSearch.java b/src/searching/JumpSearch.java deleted file mode 100644 index 4e797c3c2c47..000000000000 --- a/src/searching/JumpSearch.java +++ /dev/null @@ -1,40 +0,0 @@ -package searching; - -/** - * Implementation of Jump Search algorithm. - * - * Time Complexity: O(√n) - * Space Complexity: O(1) - * - * Reference: https://en.wikipedia.org/wiki/Jump_search - */ - -public class JumpSearch { - - public static int jumpSearch(int[] arr, int target) { - int n = arr.length; - int step = (int) Math.floor(Math.sqrt(n)); - int prev = 0; - - while (arr[Math.min(step, n)-1] < target) { - prev = step; - step += Math.floor(Math.sqrt(n)); - if (prev >= n) - return -1; - } - - for (int i = prev; i < Math.min(step, n); i++) { - if (arr[i] == target) - return i; - } - - return -1; - } - - public static void main(String[] args) { - int[] arr = {1, 3, 5, 7, 9, 12, 17, 21, 25}; - int target = 12; - int index = jumpSearch(arr, target); - System.out.println("Found at index: " + index); - } -} From 2c5717fa171aa465895b3628c974ed6e5016ec5c Mon Sep 17 00:00:00 2001 From: Fahham29 Date: Fri, 18 Jul 2025 11:09:14 +0530 Subject: [PATCH 04/11] Fix formatting and structure for Jump Search --- src/main/java/com/thealgorithms/searches/JumpSearch.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/searches/JumpSearch.java b/src/main/java/com/thealgorithms/searches/JumpSearch.java index a599d5baa27e..4dc16a9e35dd 100644 --- a/src/main/java/com/thealgorithms/searches/JumpSearch.java +++ b/src/main/java/com/thealgorithms/searches/JumpSearch.java @@ -18,7 +18,7 @@ public static int jumpSearch(int[] arr, int target) { while (arr[Math.min(step, n) - 1] < target) { prev = step; - step += Math.floor(Math.sqrt(n)); + step += Math.floor(Math.sqrt(n)); if (prev >= n) return -1; } From 49a2d00e17a8216bb7ee6c914b1c19db37a18a61 Mon Sep 17 00:00:00 2001 From: Fahham29 Date: Fri, 18 Jul 2025 11:14:08 +0530 Subject: [PATCH 05/11] Fix formatting as per CI linter rules --- .../java/com/thealgorithms/searches/JumpSearch.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/thealgorithms/searches/JumpSearch.java b/src/main/java/com/thealgorithms/searches/JumpSearch.java index 4dc16a9e35dd..ee08931e285c 100644 --- a/src/main/java/com/thealgorithms/searches/JumpSearch.java +++ b/src/main/java/com/thealgorithms/searches/JumpSearch.java @@ -8,7 +8,6 @@ * * Reference: https://en.wikipedia.org/wiki/Jump_search */ - public class JumpSearch { public static int jumpSearch(int[] arr, int target) { @@ -18,21 +17,23 @@ public static int jumpSearch(int[] arr, int target) { while (arr[Math.min(step, n) - 1] < target) { prev = step; - step += Math.floor(Math.sqrt(n)); - if (prev >= n) + step += Math.floor(Math.sqrt(n)); + if (prev >= n) { return -1; + } } for (int i = prev; i < Math.min(step, n); i++) { - if (arr[i] == target) + if (arr[i] == target) { return i; + } } return -1; } public static void main(String[] args) { - int[] arr = { 1, 3, 5, 7, 9, 12, 17, 21, 25 }; + int[] arr = {1, 3, 5, 7, 9, 12, 17, 21, 25}; int target = 12; int index = jumpSearch(arr, target); System.out.println("Found at index: " + index); From 318d2bc75fe9db4442a3def5aacc1441707dcca5 Mon Sep 17 00:00:00 2001 From: Fahham29 Date: Fri, 18 Jul 2025 11:16:58 +0530 Subject: [PATCH 06/11] Remove main method and apply final formatting for Jump Search --- .../thealgorithms/searches/JumpSearch.java | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/thealgorithms/searches/JumpSearch.java b/src/main/java/com/thealgorithms/searches/JumpSearch.java index ee08931e285c..db6e8a9c2df9 100644 --- a/src/main/java/com/thealgorithms/searches/JumpSearch.java +++ b/src/main/java/com/thealgorithms/searches/JumpSearch.java @@ -10,17 +10,21 @@ */ public class JumpSearch { + /** + * Performs jump search on a sorted array. + * + * @param arr sorted array of integers + * @param target the element to find + * @return index of target if found, else -1 + */ public static int jumpSearch(int[] arr, int target) { int n = arr.length; int step = (int) Math.floor(Math.sqrt(n)); int prev = 0; - while (arr[Math.min(step, n) - 1] < target) { + while (prev < n && arr[Math.min(step, n) - 1] < target) { prev = step; step += Math.floor(Math.sqrt(n)); - if (prev >= n) { - return -1; - } } for (int i = prev; i < Math.min(step, n); i++) { @@ -31,11 +35,4 @@ public static int jumpSearch(int[] arr, int target) { return -1; } - - public static void main(String[] args) { - int[] arr = {1, 3, 5, 7, 9, 12, 17, 21, 25}; - int target = 12; - int index = jumpSearch(arr, target); - System.out.println("Found at index: " + index); - } } From 4d4ab197ee01244ae0356d777a166b7ae231eeb7 Mon Sep 17 00:00:00 2001 From: Fahham29 Date: Fri, 18 Jul 2025 11:22:31 +0530 Subject: [PATCH 07/11] Fix warning: explicit cast from double to int --- src/main/java/com/thealgorithms/searches/JumpSearch.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/searches/JumpSearch.java b/src/main/java/com/thealgorithms/searches/JumpSearch.java index db6e8a9c2df9..a37adeff8fea 100644 --- a/src/main/java/com/thealgorithms/searches/JumpSearch.java +++ b/src/main/java/com/thealgorithms/searches/JumpSearch.java @@ -24,7 +24,8 @@ public static int jumpSearch(int[] arr, int target) { while (prev < n && arr[Math.min(step, n) - 1] < target) { prev = step; - step += Math.floor(Math.sqrt(n)); + step += (int) Math.floor(Math.sqrt(n)); + } for (int i = prev; i < Math.min(step, n); i++) { From 4b444f441493a0d702f028a88257e6795365166d Mon Sep 17 00:00:00 2001 From: Fahham29 Date: Fri, 18 Jul 2025 11:27:58 +0530 Subject: [PATCH 08/11] Fix syntax and add find(Integer[], Integer) method --- .../com/thealgorithms/searches/JumpSearch.java | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/searches/JumpSearch.java b/src/main/java/com/thealgorithms/searches/JumpSearch.java index a37adeff8fea..f3c92297fe67 100644 --- a/src/main/java/com/thealgorithms/searches/JumpSearch.java +++ b/src/main/java/com/thealgorithms/searches/JumpSearch.java @@ -25,7 +25,6 @@ public static int jumpSearch(int[] arr, int target) { while (prev < n && arr[Math.min(step, n) - 1] < target) { prev = step; step += (int) Math.floor(Math.sqrt(n)); - } for (int i = prev; i < Math.min(step, n); i++) { @@ -36,4 +35,19 @@ public static int jumpSearch(int[] arr, int target) { return -1; } + + /** + * Wrapper method to support Integer[] for testing purposes. + * + * @param arr array of Integers + * @param target target value + * @return index if found, else -1 + */ + public static int find(Integer[] arr, Integer target) { + int[] array = new int[arr.length]; + for (int i = 0; i < arr.length; i++) { + array[i] = arr[i]; + } + return jumpSearch(array, target); + } } From a7ba4ed857b8ed84278e0c7ef8c5b660c42a2b82 Mon Sep 17 00:00:00 2001 From: Fahham29 Date: Fri, 18 Jul 2025 11:43:14 +0530 Subject: [PATCH 09/11] Fix: call static method correctly in test file --- .../searches/JumpSearchTest.java | 65 +++++-------------- 1 file changed, 17 insertions(+), 48 deletions(-) diff --git a/src/test/java/com/thealgorithms/searches/JumpSearchTest.java b/src/test/java/com/thealgorithms/searches/JumpSearchTest.java index 3fa319b66a41..93c321b1bcd2 100644 --- a/src/test/java/com/thealgorithms/searches/JumpSearchTest.java +++ b/src/test/java/com/thealgorithms/searches/JumpSearchTest.java @@ -4,91 +4,60 @@ 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."); + Integer key = 5; + assertEquals(5, JumpSearch.find(array, key)); } - /** - * 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."); + Integer key = 0; + assertEquals(0, JumpSearch.find(array, key)); } - /** - * 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."); + Integer key = 10; + assertEquals(10, JumpSearch.find(array, key)); } - /** - * 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."); + Integer key = -1; + assertEquals(-1, JumpSearch.find(array, key)); } - /** - * 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."); + Integer[] array = {}; + Integer key = 1; + assertEquals(-1, JumpSearch.find(array, key)); } - /** - * 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 + array[i] = i * 2; } - Integer key = 256; // Present in the array - assertEquals(128, jumpSearch.find(array, key), "The index of the found element should be 128."); + Integer key = 256; + assertEquals(128, JumpSearch.find(array, key)); } - /** - * 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 + array[i] = i * 2; } - Integer key = 999; // Key not present - assertEquals(-1, jumpSearch.find(array, key), "The element should not be found in the array."); + Integer key = 999; + assertEquals(-1, JumpSearch.find(array, key)); } } From 2994ade8acd529fcc94d2583a7b60f549bf28a35 Mon Sep 17 00:00:00 2001 From: Fahham29 Date: Fri, 18 Jul 2025 11:48:56 +0530 Subject: [PATCH 10/11] Fix: prevent instantiation of utility class JumpSearch --- .../thealgorithms/searches/JumpSearch.java | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/thealgorithms/searches/JumpSearch.java b/src/main/java/com/thealgorithms/searches/JumpSearch.java index f3c92297fe67..db58922fdb44 100644 --- a/src/main/java/com/thealgorithms/searches/JumpSearch.java +++ b/src/main/java/com/thealgorithms/searches/JumpSearch.java @@ -10,13 +10,11 @@ */ public class JumpSearch { - /** - * Performs jump search on a sorted array. - * - * @param arr sorted array of integers - * @param target the element to find - * @return index of target if found, else -1 - */ + // Prevent instantiation + private JumpSearch() { + throw new UnsupportedOperationException("Utility class"); + } + public static int jumpSearch(int[] arr, int target) { int n = arr.length; int step = (int) Math.floor(Math.sqrt(n)); @@ -36,13 +34,6 @@ public static int jumpSearch(int[] arr, int target) { return -1; } - /** - * Wrapper method to support Integer[] for testing purposes. - * - * @param arr array of Integers - * @param target target value - * @return index if found, else -1 - */ public static int find(Integer[] arr, Integer target) { int[] array = new int[arr.length]; for (int i = 0; i < arr.length; i++) { From 96e036e8d9282e9dfb1976a18184633c37711240 Mon Sep 17 00:00:00 2001 From: Fahham29 Date: Fri, 18 Jul 2025 11:54:04 +0530 Subject: [PATCH 11/11] Fix: mark JumpSearch as final utility class --- src/main/java/com/thealgorithms/searches/JumpSearch.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/searches/JumpSearch.java b/src/main/java/com/thealgorithms/searches/JumpSearch.java index db58922fdb44..7ab8b35b3202 100644 --- a/src/main/java/com/thealgorithms/searches/JumpSearch.java +++ b/src/main/java/com/thealgorithms/searches/JumpSearch.java @@ -8,7 +8,7 @@ * * Reference: https://en.wikipedia.org/wiki/Jump_search */ -public class JumpSearch { +public final class JumpSearch { // Prevent instantiation private JumpSearch() {