Skip to content

Commit bdbbece

Browse files
authored
Improve JumpSearch documentation with detailed explanation and example (#7354)
Issue: #7349 Added: - Step-by-step algorithm explanation - Worked example with input/output - Time and space complexity analysis - Notes on when to use Jump Search vs Binary/Linear Search - @see references to related search algorithms
1 parent 5753f46 commit bdbbece

File tree

1 file changed

+32
-8
lines changed

1 file changed

+32
-8
lines changed

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)