Like Binary 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 skipping some elements in place of searching all elements.

For example, suppose we have an array arr[] of size n and block (to be jumped) size m. Then we search at the indexes arr[0], arr[m], arr[2m]…..arr[km] and so on. Once we find the interval (arr[km] < x < arr[(k+1)m]), we perform a linear search operation from the index km to find the element x.

Let’s consider the following array: (0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610). Length of the array is 16. Jump search will find the value of 55 with the following steps assuming that the block size to be jumped is 4.

STEP 1: Jump from index 0 to index 4;
STEP 2: Jump from index 4 to index 8;
STEP 3: Jump from index 8 to index 16;
STEP 4: Since the element at index 16 is greater than 55 we will jump back a step to come to index 9.
STEP 5: Perform linear search from index 9 to get the element 55.

[ad type=”banner”]

What is the optimal block size to be skipped?

In the worst case, we have to do n/m jumps and if the last checked value is greater than the element to be searched for, we perform m-1 comparisons more for linear search. Therefore the total number of comparisons in the worst case will be ((n/m) + m-1). The value of the function ((n/m) + m-1) will be minimum when m = √n. Therefore, the best step size is m = √n.

Java Implementation :

[pastacode lang=”java” manual=”%2F%2F%20Java%20program%20to%20implement%20Jump%20Search.%0Apublic%20class%20JumpSearch%0A%7B%0A%20%20%20%20public%20static%20int%20jumpSearch(int%5B%5D%20arr%2C%20int%20x)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20int%20n%20%3D%20arr.length%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Finding%20block%20size%20to%20be%20jumped%0A%20%20%20%20%20%20%20%20int%20step%20%3D%20(int)Math.floor(Math.sqrt(n))%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Finding%20the%20block%20where%20element%20is%0A%20%20%20%20%20%20%20%20%2F%2F%20present%20(if%20it%20is%20present)%0A%20%20%20%20%20%20%20%20int%20prev%20%3D%200%3B%0A%20%20%20%20%20%20%20%20while%20(arr%5BMath.min(step%2C%20n)-1%5D%20%3C%20x)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20prev%20%3D%20step%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20step%20%2B%3D%20(int)Math.floor(Math.sqrt(n))%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(prev%20%3E%3D%20n)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20return%20-1%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Doing%20a%20linear%20search%20for%20x%20in%20block%0A%20%20%20%20%20%20%20%20%2F%2F%20beginning%20with%20prev.%0A%20%20%20%20%20%20%20%20while%20(arr%5Bprev%5D%20%3C%20x)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20prev%2B%2B%3B%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20If%20we%20reached%20next%20block%20or%20end%20of%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20array%2C%20element%20is%20not%20present.%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(prev%20%3D%3D%20Math.min(step%2C%20n))%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20return%20-1%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20If%20element%20is%20found%0A%20%20%20%20%20%20%20%20if%20(arr%5Bprev%5D%20%3D%3D%20x)%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20prev%3B%0A%20%0A%20%20%20%20%20%20%20%20return%20-1%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20Driver%20program%20to%20test%20function%0A%20%20%20%20public%20static%20void%20main(String%20%5B%20%5D%20args)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20int%20arr%5B%5D%20%3D%20%7B%200%2C%201%2C%201%2C%202%2C%203%2C%205%2C%208%2C%2013%2C%2021%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2034%2C%2055%2C%2089%2C%20144%2C%20233%2C%20377%2C%20610%7D%3B%0A%20%20%20%20%20%20%20%20int%20x%20%3D%2055%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Find%20the%20index%20of%20’x’%20using%20Jump%20Search%0A%20%20%20%20%20%20%20%20int%20index%20%3D%20jumpSearch(arr%2C%20x)%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Print%20the%20index%20where%20’x’%20is%20located%0A%20%20%20%20%20%20%20%20System.out.println(%22%5CnNumber%20%22%20%2B%20x%20%2B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22%20is%20at%20index%20%22%20%2B%20index)%3B%0A%20%20%20%20%7D%0A%7D” message=”java” highlight=”” provider=”manual”/]

Output

Number 55 is at index 10.

Time Complexity : O(√n)

Auxiliary Space : O(1)

[ad type=”banner”]

Important points:

  • Works only sorted arrays.
  • The optimal size of a block to be jumped is O(√ n). This makes the time complexity of Jump Search O(√ n).
  • The time complexity of Jump Search is between Linear Search ( ( O(n) ) and Binary Search ( O (Log n) ).
  • Binary Search is better than Jump Search, but Jump search has an advantage that we traverse back only once (Binary Search may require up to O(Log n) jumps, consider a situation where the element to be search is the smallest element or smaller than the smallest). So in a systems where jumping back is costly, we use Jump Search.