committed by
Randell Dawson
parent
d7be1802e7
commit
12ef1b7925
@ -17,6 +17,42 @@ O(√N)
|
||||
|
||||

|
||||
|
||||
# Code In Java.
|
||||
|
||||
``` Java
|
||||
|
||||
public int jumpSearch(int[] arr, int x)
|
||||
{
|
||||
int n = arr.length;
|
||||
|
||||
// Finding the size to be jumped
|
||||
int jumpSize = (int) Math.floor(Math.sqrt(n));
|
||||
|
||||
// Finding the index where element is present
|
||||
int index = 0;
|
||||
while (arr[Math.min(jumpSize, n)-1] < x)
|
||||
{
|
||||
index = jumpSize;
|
||||
jumpSize += (int) Math.floor(Math.sqrt(n));
|
||||
if (index >= n)
|
||||
return -1;
|
||||
}
|
||||
// Searching for x beginning with index.
|
||||
while (arr[index] < x)
|
||||
{
|
||||
index++;
|
||||
// If we reached next index or end of array then element is not present.
|
||||
if (index == Math.min(jumpSize, n))
|
||||
return -1;
|
||||
}
|
||||
// If element is found
|
||||
if (arr[index] == x)
|
||||
return index;
|
||||
return -1;
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
# Code
|
||||
To view examples of code implementation for this method, access this link below:
|
||||
|
||||
|
Reference in New Issue
Block a user