# Jump Search

Searching AlgorithmAlgorithmsData Structure and Algorithms

Jump search technique also works for ordered lists. It creates a block and tries to find the element in that block. If the item is not in the block, it shifts the entire block. The block size is based on the size of the list. If the size of the list is n then block size will be √n. After finding a correct block it finds the item using a linear search technique. The jump search lies between linear search and binary search according to its performance.

## The complexity of Jump Search Technique

• Time Complexity: O(√n)
• Space Complexity: O(1)

## Input and Output

Input:
A sorted list of data:
10 13 15 26 28 50 56 88 94 127 159 356 480 567 689 699 780 850 956 995
The search key 356
Output:
Item found at location: 11

## Algorithm

jumpSearch(array, size, key)

Input: An sorted array, size of the array and the search key

Output − location of the key (if found), otherwise wrong location.

Begin
blockSize := √size
start := 0
end := blockSize
while array[end] <= key AND end < size do
start := end
end := end + blockSize
if end > size – 1 then
end := size
done
for i := start to end -1 do
if array[i] = key then
return i
done
return invalid location
End

## Example

#include<iostream>
#include<cmath>

using namespace std;
int jumpSearch(int array[], int size, int key) {
int start = 0;
int end = sqrt(size); //the square root of array length

while(array[end] <= key && end < size) {
start = end; //it it is not correct block then shift block
end += sqrt(size);
if(end > size - 1)
end = size; //if right exceeds then bound the range
}

for(int i = start; i<end; i++) { //perform linear search in selected block
if(array[i] == key)
return i; //the correct position of the key
}
return -1;
}

int main() {
int n, searchKey, loc;
cout << "Enter number of items: ";
cin >> n;
int arr[n]; //create an array of size n
cout << "Enter items: " << endl;

for(int i = 0; i< n; i++) {
cin >> arr[i];
}

cout << "Enter search key to search in the list: ";
cin >> searchKey;
if((loc = jumpSearch(arr, n, searchKey)) >= 0)
cout << "Item found at location: " << loc << endl;
else
}
Enter number of items: 20
Item found at location: 11