Skip to content

Commit 318d2bc

Browse files
author
Fahham29
committed
Remove main method and apply final formatting for Jump Search
1 parent 49a2d00 commit 318d2bc

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

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

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,21 @@
1010
*/
1111
public class JumpSearch {
1212

13+
/**
14+
* Performs jump search on a sorted array.
15+
*
16+
* @param arr sorted array of integers
17+
* @param target the element to find
18+
* @return index of target if found, else -1
19+
*/
1320
public static int jumpSearch(int[] arr, int target) {
1421
int n = arr.length;
1522
int step = (int) Math.floor(Math.sqrt(n));
1623
int prev = 0;
1724

18-
while (arr[Math.min(step, n) - 1] < target) {
25+
while (prev < n && arr[Math.min(step, n) - 1] < target) {
1926
prev = step;
2027
step += Math.floor(Math.sqrt(n));
21-
if (prev >= n) {
22-
return -1;
23-
}
2428
}
2529

2630
for (int i = prev; i < Math.min(step, n); i++) {
@@ -31,11 +35,4 @@ public static int jumpSearch(int[] arr, int target) {
3135

3236
return -1;
3337
}
34-
35-
public static void main(String[] args) {
36-
int[] arr = {1, 3, 5, 7, 9, 12, 17, 21, 25};
37-
int target = 12;
38-
int index = jumpSearch(arr, target);
39-
System.out.println("Found at index: " + index);
40-
}
4138
}

0 commit comments

Comments
 (0)