Skip to content

Commit ba7cb8f

Browse files
author
Fahham29
committed
Fix structure and package for Jump Search algorithm
1 parent 5d9eb00 commit ba7cb8f

File tree

2 files changed

+25
-81
lines changed

2 files changed

+25
-81
lines changed
Lines changed: 25 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,40 @@
11
package com.thealgorithms.searches;
22

3-
import com.thealgorithms.devutils.searches.SearchAlgorithm;
4-
53
/**
6-
* An implementation of the Jump Search algorithm.
7-
*
8-
* <p>
9-
* Jump Search is an algorithm for searching sorted arrays. It works by dividing the array
10-
* into blocks of a fixed size (the block size is typically the square root of the array length)
11-
* and jumping ahead by this block size to find a range where the target element may be located.
12-
* Once the range is found, a linear search is performed within that block.
4+
* Implementation of Jump Search algorithm.
135
*
14-
* <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.
6+
* Time Complexity: O(√n)
7+
* Space Complexity: O(1)
178
*
18-
* <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-
*
24-
* <p>
25-
* This class implements the {@link SearchAlgorithm} interface, providing a generic search method
26-
* for any comparable type.
9+
* Reference: https://en.wikipedia.org/wiki/Jump_search
2710
*/
28-
public class JumpSearch implements SearchAlgorithm {
2911

30-
/**
31-
* Jump Search algorithm implementation.
32-
*
33-
* @param array the sorted array containing elements
34-
* @param key the element to be searched
35-
* @return the index of {@code key} if found, otherwise -1
36-
*/
37-
@Override
38-
public <T extends Comparable<T>> int find(T[] array, T key) {
39-
int length = array.length;
40-
int blockSize = (int) Math.sqrt(length);
12+
public class JumpSearch {
13+
14+
public static int jumpSearch(int[] arr, int target) {
15+
int n = arr.length;
16+
int step = (int) Math.floor(Math.sqrt(n));
17+
int prev = 0;
4118

42-
int limit = blockSize;
43-
// Jumping ahead to find the block where the key may be located
44-
while (limit < length && key.compareTo(array[limit]) > 0) {
45-
limit = Math.min(limit + blockSize, length - 1);
19+
while (arr[Math.min(step, n) - 1] < target) {
20+
prev = step;
21+
step += Math.floor(Math.sqrt(n));
22+
if (prev >= n)
23+
return -1;
4624
}
4725

48-
// Perform linear search within the identified block
49-
for (int i = limit - blockSize; i <= limit && i < length; i++) {
50-
if (array[i].equals(key)) {
26+
for (int i = prev; i < Math.min(step, n); i++) {
27+
if (arr[i] == target)
5128
return i;
52-
}
5329
}
30+
5431
return -1;
5532
}
33+
34+
public static void main(String[] args) {
35+
int[] arr = { 1, 3, 5, 7, 9, 12, 17, 21, 25 };
36+
int target = 12;
37+
int index = jumpSearch(arr, target);
38+
System.out.println("Found at index: " + index);
39+
}
5640
}

src/searching/JumpSearch.java

Lines changed: 0 additions & 40 deletions
This file was deleted.

0 commit comments

Comments
 (0)