-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJumpSearch.java
More file actions
45 lines (37 loc) · 746 Bytes
/
JumpSearch.java
File metadata and controls
45 lines (37 loc) · 746 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
36
37
38
39
40
41
42
43
44
45
package geeks.algo.search;
public class JumpSearch extends Search {
@Override
boolean search() {
boolean found = false;
int n = this.inputArr.length;
int m = ((Double)Math.sqrt(n)).intValue();
for(int i=m;i<n;i=i+m){
if(this.inputArr[i]>this.key){
int left = i-m;
int right = i;
while(left<=right){
int mid = left + (right-left)/2;
if(this.inputArr[mid]==key){
found=true;
break;
}
if(this.inputArr[mid]<key){
left =mid+1;
}else{
right= mid-1;
}
}
}
if(found){
break;
}
}
return found;
}
@Override
void setComplexities() {
this.timeComplexity ="O(sqrt(n))";
this.spaceComplexity="O(1)";
this.type="Jump Search";
}
}