What is jump search?

Searching Algorithms Questions Medium



24 Short 58 Medium 71 Long Answer Questions Question Index

What is jump search?

Jump search is a searching algorithm that is used to find the position of a target value within a sorted array. It is an improvement over linear search as it reduces the number of comparisons required.

The jump search algorithm works by dividing the array into smaller blocks or subarrays of equal size. It then performs a linear search on each block to determine if the target value is present. If the target value is found within a block, the search is considered successful. However, if the target value is not found within a block, the algorithm jumps to the next block and continues the search.

To implement jump search, the following steps are followed:
1. Determine the block size by taking the square root of the array length.
2. Start at the first element of the array and compare it with the target value.
3. If the target value is found, return the index of the element.
4. If the target value is greater than the current element, jump to the next block by incrementing the index by the block size.
5. Repeat steps 3 and 4 until the target value is found or the current element is greater than the target value.
6. If the current element is greater than the target value, perform a linear search within the previous block to find the target value.
7. If the target value is found, return the index of the element. Otherwise, return -1 to indicate that the target value is not present in the array.

Jump search has a time complexity of O(sqrt(n)), where n is the size of the array. This makes it more efficient than linear search for large arrays, but less efficient than binary search. It is particularly useful when the array is sorted and uniformly distributed.