forked from Annex5061/java-algorithms
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaAlgo.java
More file actions
Latest commit
35 lines (27 loc) · 927 Bytes
/
Copy pathJavaAlgo.java
File metadata and controls
35 lines (27 loc) · 927 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
28
29
30
31
32
33
34
35
publicclassJumpSearchAlgorithm {
publicstaticvoidmain(String[] args) {
int[] arr = {10, 20, 30, 40, 50, 60, 70, 80, 90};
intele = 60;
intfoundIndex = jumpSearch(arr, ele);
System.out.println(foundIndex > 0 ? "Found at index : " + foundIndex : "Element Not Found");
}
publicstaticintjumpSearch(int[] arr, intele) {
intprev = 0;
intn = arr.length;
intstep = (int) Math.floor(Math.sqrt(n));
//loop until current element is less than the given search element
while (arr[Math.min(step, n) - 1] < ele) {
prev = step;
step += (int) Math.floor(Math.sqrt(n));
if (prev >= n) return -1;
}
//perform linear search prev index element to given element
while (arr[prev] < ele) {
prev++;
if (prev == Math.min(step, n)) return -1;
}
// Return index if element is found
if (arr[prev] == ele) returnprev;
return -1;
}
}