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 a block (to be jumped) of size m. Then we search in the indexes arr, 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). The length of the array is 16. The 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 12;
STEP 4: Since the element at index 12 is greater than 55, we will jump back a step to come to index 8.
STEP 5: Perform a linear search from index 8 to get the element 55.
Performance in comparison to linear and binary search:
If we compare it with linear and binary search then it comes out then it is better than linear search but not better than binary search.
The increasing order of performance is:
linear search < jump search < binary search
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.
- Jump Search is an algorithm for finding a specific value in a sorted array by jumping through certain steps in the array.
- The steps are determined by the sqrt of the length of the array.
- Here is a step-by-step algorithm for the jump search:
- Determine the step size m by taking the sqrt of the length of the array n.
- Start at the first element of the array and jump m steps until the value at that position is greater than the target value.
Once a value greater than the target is found, perform a linear search starting from the previous step until the target is found or it is clear that the target is not in the array.
If the target is found, return its index. If not, return -1 to indicate that the target was not found in the array.
Example 1 :
Number 55 is at index 10
Time Complexity : O(√n)
Auxiliary Space : O(1)
Advantages of Jump Search:
- Better than a linear search for arrays where the elements are uniformly distributed.
- Jump search has a lower time complexity compared to a linear search for large arrays.
- The number of steps taken in jump search is proportional to the square root of the size of the array, making it more efficient for large arrays.
- It is easier to implement compared to other search algorithms like binary search or ternary search.
- Jump search works well for arrays where the elements are in order and uniformly distributed, as it can jump to a closer position in the array with each iteration.
- Works only with sorted arrays.
- The optimal size of a block to be jumped is (√ 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 the 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 searched is the smallest element or just bigger than the smallest). So, in a system where binary search is costly, we use Jump Search.
This article is contributed by Harsh Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Please Login to comment...