Analysis and Design of
Algorithms
Jump Search
4th semester (morning)
JUMP SEARCH
Jump Search is a searching algorithm for sorted arrays. The basic
idea is to check fewer elements (than linear search) by jumping
ahead by fixed steps or JUMP SEARCH
skipping some elements in place of searching
all elements.
JUMP SEARCH
Key Points to remember about Jump Search Algorithm
▪ This algorithm works only for sorted input arrays
▪ Optimal size of the block to be skipped is √n, thus resulting in the
time complexity O(√n2)
▪ The time complexity of this algorithm lies in between linear search
(O(n)) and binary search (O(log n))
▪ It is also called block search algorithm
JUMP SEARCH
Complexity Analysis for Jump Search
▪ Time Complexity:
Code executes n/m times because the loop counter increments by m
times in every iteration. Since the optimal value of m= √n , thus, n/m=√n
resulting in a time complexity of O(√n).
▪ Space Complexity:
The space complexity of this algorithm is O(1) since it does not require
any other data structure for its implementation
JUMP SEARCH
▪ Algorithm:
• Step1: Calculate Jump size
• Step 2: jump from index I to index i+ jump
• Step3: if x== arr [ i+ jimp] return x
• Else jump back a step
• Step4: perform linear search
JUMP SEARCH
▪ Assume the following sorted array:
▪ Search for 77
0 1 1 2 3 5 8 13 21 34 55 77 89 91 95 110
JUMP SEARCH
• Calculate :
• Size of array n = 16
• Jump size = sqrt(n)=4
0 1 1 2 3 5 8 13 21 34 55 77 89 91 95 110
JUMP SEARCH
▪ Jump size = 4
▪ Search from index 0
▪ Compare index value with search value number 0 < 77 77
0 1 1 2 3 5 8 13 21 34 55 77 89 91 95 110
JUMP SEARCH
▪ Jump size = 4
▪ Jump from index 0 to index 3
▪ Compare index value with search value number 2< 77 77
0 1 1 2 3 5 8 13 21 34 55 77 89 91 95 110
JUMP SEARCH
▪ Jump size = 4
▪ Jump from index 3 to index 6
▪ Compare index value with search value number 8< 77 77
0 1 1 2 3 5 8 13 21 34 55 77 89 91 95 110
JUMP SEARCH
▪ Jump size = 4
▪ Jump from index 6 to index 9
▪ Compare index value with search value number 34< 77 77
0 1 1 2 3 5 8 13 21 34 55 77 89 91 95 110
JUMP SEARCH
▪ Jump size = 4
▪ Jump from index 9 to index 12
▪ Compare index value with search value number 89< 77 77
0 1 1 2 3 5 8 13 21 34 55 77 89 91 95 110
JUMP SEARCH
▪ Jump back a step
▪ Perform linear search
▪ Compare found at index 11 77
0 1 1 2 3 5 8 13 21 34 55 77 89 91 95 110
Implementatiopn of jump search
#include<iostream>
#include<cmath>
using namespace std;
int jump_Search(int a[], int n, int item) {
int i = 0;
int m = sqrt(n); //initializing block size= √(n)
while(a[m] <= item && m < n) {
// the control will continue to jump the blocks
i = m; // shift the block
Implementatiopn of jump search
for(int x = i; x<m; x++) { //linear search in current block
if(a[x] == item)
return x; //position of element being searched
}
return -1;}
int main() {
int n, item, loc;
cout << "\n Enter number of items: ";
cin >> n;
int arr[n]; //creating an array of size n
cout << "\n Enter items: ";
for(int i = 0; i< n; i++) {
cin >> arr[i];
}
Implementatiopn of jump search
cout << "\n Enter search key to be found in the array: ";
cin >> item;
loc = jump_Search(arr, n, item);
if(loc>=0)
cout << "\n Item found at location: " << loc;
else
cout << "\n Item is not found in the list.";
}
Advantages of jump search
▪ It is faster than the linear search technique which has a time
complexity of O(n) for searching an element
Disadvantages of jump search
▪ It is slower than binary search algorithm which searches an element
in O(log n)
▪ It requires the input array to be sorted