forked from iRupam/NewtonSchoolInfinityJune21
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJumpSearch.java
More file actions
48 lines (37 loc) · 1.1 KB
/
JumpSearch.java
File metadata and controls
48 lines (37 loc) · 1.1 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
44
45
46
47
48
package InfinityJune21.SearchTechniques;
public class JumpSearch {
public static int jumpSearch(int arr[], int key) {
int length = arr.length;
int step = (int)Math.sqrt(length);
int prev = 0;
while(arr[Math.min(step, length) - 1] < key) {
prev = step;
step = step + (int)Math.sqrt(length);
if(prev >= length) {
return -1;
}
}
while(arr[prev] < key) {
prev++;
if(prev == (Math.min(step, length)) - 1) {
return -1;
}
}
if(arr[prev] == key) {
return prev;
}
return -1;
}
public static void main(String[] args) {
int arr[] = {0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89,
144, 233, 377, 610};
int key = 1;
int index = jumpSearch(arr, key);
if(index != -1) {
System.out.println("Element found at index " + index);
}
else {
System.out.println("Element not found");
}
}
}