Jump Search

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.

Visualization

Elements seperated with (',') : ( Eg: 34,54,23,45,23,12,76,1,999 )

Speed : 0.5 to 4 secs ( default = 2 secs )
Search (only single element) :

Status :

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: Since, its already sorted, We directly begin from the jump process. 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.

This could be understand better by viewing the visualization Above.

Explaination


Consider an array, in the example given above.
So, Elements are : 34,54,23,45,23,12,76,1,999
Search Element : 34

Step 1 : Sorting and jump from initial index[0] to [3]

Since, jump search works on sorted arrays, hence the first most step is to sort the entire array. After Sorting, since, the size of the array is 9, hence, the shifts would be the square root of the size, floored to the value.

Step 2 : Jump from [3] to [6]

Since, Jump = previous index[3] + square of size

Step 3 : Check Limiting Condition

Since, the next consecutive Jump will lead to 'out of bounds', hence the Jumping process will cease. Now, there are two cases possible. The search Number could either be greater, or less than the present index. Since here, we have search element less than the present element at the index, the array would undergo a backloop with single jumps, until the element is found, till the tail of the Bigger arrow. If the element is still not found, then the search element is not present in the given array.
So, the forward loop halts at index[6], and a back loop starts, till the element to be searched is encountered or the tail is reached.


Hence, the Element 34 was found at index[4]