Skip to content

Commit 4c1e80e

Browse files
Merge branch 'master' into 7350-binary-search-reliability-improvement
2 parents ae637be + bdbbece commit 4c1e80e

File tree

2 files changed

+34
-10
lines changed

2 files changed

+34
-10
lines changed

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,14 @@
112112
<dependency>
113113
<groupId>com.puppycrawl.tools</groupId>
114114
<artifactId>checkstyle</artifactId>
115-
<version>13.3.0</version>
115+
<version>13.4.0</version>
116116
</dependency>
117117
</dependencies>
118118
</plugin>
119119
<plugin>
120120
<groupId>com.github.spotbugs</groupId>
121121
<artifactId>spotbugs-maven-plugin</artifactId>
122-
<version>4.9.8.2</version>
122+
<version>4.9.8.3</version>
123123
<configuration>
124124
<excludeFilterFile>spotbugs-exclude.xml</excludeFilterFile>
125125
<includeTests>true</includeTests>

src/main/java/com/thealgorithms/searches/JumpSearch.java

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,50 @@
1212
* Once the range is found, a linear search is performed within that block.
1313
*
1414
* <p>
15-
* The Jump Search algorithm is particularly effective for large sorted arrays where the cost of
16-
* performing a linear search on the entire array would be prohibitive.
15+
* <b>How it works:</b>
16+
* <ol>
17+
* <li>Calculate the optimal block size as √n (square root of array length)</li>
18+
* <li>Jump ahead by the block size until the current element is greater than the target</li>
19+
* <li>Perform a linear search backwards within the identified block</li>
20+
* </ol>
1721
*
1822
* <p>
19-
* Worst-case performance: O(√N)<br>
20-
* Best-case performance: O(1)<br>
21-
* Average performance: O(√N)<br>
22-
* Worst-case space complexity: O(1)
23+
* <b>Example:</b><br>
24+
* Array: [1, 3, 5, 7, 9, 11, 13, 15, 17, 19], Target: 9<br>
25+
* Step 1: Jump from index 0 → 3 → 6 (9 < 13, so we found the block)<br>
26+
* Step 2: Linear search from index 3 to 6: found 9 at index 4<br>
27+
* Result: Index = 4
28+
*
29+
* <p>
30+
* <b>Time Complexity:</b><br>
31+
* - Best-case: O(1) - element found at first position<br>
32+
* - Average: O(√n) - optimal block size reduces jumps<br>
33+
* - Worst-case: O(√n) - element at end of array or not present<br>
34+
*
35+
* <p>
36+
* <b>Space Complexity:</b> O(1) - only uses a constant amount of extra space
37+
*
38+
* <p>
39+
* <b>Note:</b> Jump Search requires a sorted array. For unsorted arrays, use Linear Search.
40+
* Compared to Linear Search (O(n)), Jump Search is faster for large arrays.
41+
* Compared to Binary Search (O(log n)), Jump Search is less efficient but may be
42+
* preferable when jumping through a linked list or when backward scanning is costly.
2343
*
2444
* <p>
2545
* This class implements the {@link SearchAlgorithm} interface, providing a generic search method
2646
* for any comparable type.
47+
*
48+
* @see SearchAlgorithm
49+
* @see BinarySearch
50+
* @see LinearSearch
2751
*/
2852
public class JumpSearch implements SearchAlgorithm {
2953

3054
/**
3155
* Jump Search algorithm implementation.
3256
*
33-
* @param array the sorted array containing elements
34-
* @param key the element to be searched
57+
* @param array the sorted array containing elements (must be sorted in ascending order)
58+
* @param key the element to be searched for
3559
* @return the index of {@code key} if found, otherwise -1
3660
*/
3761
@Override

0 commit comments

Comments
 (0)