-
Notifications
You must be signed in to change notification settings - Fork 99
/
jump_search.java
54 lines (45 loc) · 1.37 KB
/
jump_search.java
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
49
50
51
52
53
54
public class JumpSearch
{
public static int jumpSearch(int[] arr, int i)
{
int n = arr.length;
// Finding block size to be jumped
int step = (int)Math.floor(Math.sqrt(n));
// Finding the block where element is present
// (if it is present)
int prev = 0;
while (arr[Math.min(step, n)-1] < i)
{
prev = step;
step += (int)Math.floor(Math.sqrt(n));
if (prev >= n)
return -1;
}
// Doing a linear search for i in block
// beginning with prev.
while (arr[prev] < i)
{
prev++;
// If we reached next block or end of array
// element is not present.
if (prev == Math.min(step, n))
return -1;
}
// If element is found
if (arr[prev] == i)
return prev;
return -1;
}
// Driver program to test function
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 i = 55;
// Find the index of 'i' using Jump Search
int index = jumpSearch(arr, i);
// Print the index where 'i' is located
System.out.println("\nNumber " + i +
" is at index " + index);
}
}