forked from examplehub/Java
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJumpSearch.java
More file actions
32 lines (30 loc) · 731 Bytes
/
JumpSearch.java
File metadata and controls
32 lines (30 loc) · 731 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
package com.examplehub.searches;
public class JumpSearch implements Search {
@Override
public int search(int[] numbers, int key) {
int length = numbers.length;
int steps = (int) Math.floor(Math.sqrt(length));
int prev = 0;
while (numbers[Math.min(steps, length) - 1] < key) {
prev = steps;
steps += (int) Math.floor(Math.sqrt(length));
if (prev >= length) {
return -1;
}
}
while (numbers[prev] < key) {
prev++;
if (prev == Math.min(steps, length)) {
return -1;
}
}
if (numbers[prev] == key) {
return prev;
}
return -1;
}
@Override
public <T extends Comparable<T>> int search(T[] array, T key) {
return 0;
}
}