Jump Search

  1. Jump Search Algorithm
  2. Jump Search Example
  3. Jump Search Algorithm Implementation
  4. Jump Search Algorithm Complexity

Jump Search is an interval searching algorithm. It is a relatively new algorithm that works only on sorted arrays. It tries to reduce the number of comparisons required than linear search by not scanning every single element like linear search. In jump search, the array is divided into m blocks. It searches the element in one block and, if the element is not present, then moves to the next block. When the algorithm finds the block containing the element, it uses the linear search algorithm to find the exact index. This algorithm is faster than linear search but slower than binary search.

Perfect Jump PLAY NOW https://playi...
Perfect Jump PLAY NOW https://playingzone.in/game/perfect-jump

Jump Search Algorithm

Let us assume that we have an unsorted array A[] containing n elements, and we want to find an element X.

  • Start from the first element set i as 0 and block size m as √n.
  • While A[min(m,n)-1] < X and i < n.
    • Set i as m and increment m by √n.
  • If i >= n return -1.
  • While A[i]< X do the following:
    • increment i
    • if i is equal to min(m,n) return -1
  • If A[i] == X return i.
  • Else return -1.

Jump Search Example

Suppose we have the array: (1, 2, 3, 4, 5, 6, 7, 8, 9), and we want to find X - 7.

Since there are 9 elements, we have n as 9.

  • Set i as 0 and m as √9 that is 3.
  • A[2] is smaller than X . Set i as 3 and m as 6.
  • A[5] is smaller than X . Set i as 6 and m as 9.
  • A[8] is equal to X . Break out of the loop.
  • i as 6 is less than n.
  • A[6] == 7 . Break out of the loop
  • Since A[6] == 7, return 6.

Jump Search Algorithm Implementation

#include <bits/stdc++.h>
using namespace std;

int jumpSearch(int arr[], int x, int n)

    int m = sqrt(n);
    int i = 0;
    while (arr[min(m, n) - 1] < x)
        i = m;
        m += sqrt(n);
        if (i >= n)
            return -1;
    while (arr[i] < x)
        if (i == min(m, n))
            return -1;
    if (arr[i] == x)
        return i;

    return -1;

int main() {
    int n = 10;
    int arr[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    int x = 7;
    int result = jumpSearch(arr, x, n);
    if (result == -1) cout << "Element not found";
    else cout << "Element found at index " << result;

Jump Search Algorithm Complexity

Time Complexity

  • Average Case

The jump sort algorithm runs n/m times where n is the number of elements, and m is the block size. Linear search requires m-1 comparisons making the total time expression n/m + m-1. The most optimal value of m minimizing the time expression is √n, making the time complexity n/√n + √n, i.e. √n. The time complexity of the Jump Search Algorithm is O(√n).

  • Best Case

The best-case time complexity is O(1). It occurs when the element to be searched is the first element present inside the array.

  • Worst-Case

The worst-case occurs when we do n/m jumps, and the last value we checked is greater than the element we are searching for, and m-1 comparisons are performed for linear search. The worst-case time complexity is O(√n).

Space Complexity

This algorithm’s space complexity is O(1) because it doesn’t require any data structure other than temporary variables.

Related Article - Search Algorithm

  • Exponential Search
  • Fibonacci Search
  • Binary Search
  • Interpolation Search