-
Notifications
You must be signed in to change notification settings - Fork 183
Expand file tree
/
Copy pathJumpSearch.java
More file actions
27 lines (22 loc) · 766 Bytes
/
JumpSearch.java
File metadata and controls
27 lines (22 loc) · 766 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package Java.Searching algorithms;
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 += (int) 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, 11, 13};
int index = jumpSearch(arr, 9);
System.out.println(index != -1 ? "Found at index " + index : "Not found");
}
}