-
Notifications
You must be signed in to change notification settings - Fork 183
Expand file tree
/
Copy pathJumpSearch.java
More file actions
44 lines (35 loc) · 1.12 KB
/
JumpSearch.java
File metadata and controls
44 lines (35 loc) · 1.12 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package SearchingAlgorithms;
public class JumpSearch {
public static void main(String[] args) {
int[] arr = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19};
int target = 13;
int result = jumpSearch(arr, target);
if (result == -1)
System.out.println("Element not found");
else
System.out.println("Element found at index: " + result);
}
static int jumpSearch(int[] arr, int target) {
int n = arr.length;
// Finding block size to jump
int step = (int)Math.floor(Math.sqrt(n));
int prev = 0;
// Jump ahead while the last element of block < target
while (arr[Math.min(step, n) - 1] < target) {
prev = step;
step += (int)Math.floor(Math.sqrt(n));
if (prev >= n)
return -1;
}
// Linear search in the found block
while (arr[prev] < target) {
prev++;
if (prev == Math.min(step, n))
return -1;
}
// If element is found
if (arr[prev] == target)
return prev;
return -1;
}
}