Jump Search
Skip search is a range search algorithm. It is a relatively new algorithm that works only on sorted arrays. It tries to reduce the number of comparisons required compared to linear search by not scanning every element like linear search. In skip search, the array is divided into m
blocks. It searches for an element in a block and if the element is not present, it moves to the next block. When the algorithm finds a block containing an element, it uses a linear search algorithm to find the exact index. This algorithm is faster than linear search but slower than binary search.
Jump Search Algorithm
Suppose we have an unsorted array A[]
containing n
elements and we want to find an element X
.
- Starting from the first element, set i to 0 and the block size m to √n.
- When A[min(m,n)-1]<X and i<n.
- Set i to m, and increment m by √n.
- If i >= n return -1.
- When A[i]< X, do the following.
- Increment i
- If i is equal to min(m,n) return -1.
- If A[i] == X, return i.
- Otherwise, returns -1.
Jump Search Example
Suppose we have an array: (1, 2, 3, 4, 5, 6, 7, 8, 9)
, and we want to find X- 7
.
Since there are 9 elements, we n
set to 9
.
- Let i be 0 and m be √9, which is 3.
- A[2] is smaller than X. Let i be 3 and m be 6.
- A[5] is smaller than X. Let i be 6 and m be 9.
- A[8] equals 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, 6 is returned.
Implementation of the Jump Search Algorithm
#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)
{
i++;
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;
}
Complexity of jump search algorithm
Time Complexity
- Average situation
The skip sort algorithm runs n/m
times, where n
is the number of elements and m
is the block size. Linear search requires m-1
times comparisons, so that the total time expression is . The optimal value of that n/m+m-1
minimizes the time expression is , so that the time complexity is , that is . The time complexity of the skip search algorithm is .m
√n
n/√n+√n
√n
O(√n)
- Best Case
The best case time complexity is O(1)
. This occurs when the element being searched is the first element in the array.
- Worst case scenario
The worst case happens n/m
when we make a jump, and the last value we check is greater than the element we are searching for, m-1
so the comparison is a linear search. The worst case time complexity is O(√n)
.
Space complexity
The space complexity of this algorithm is O(1)
, since it does not require any data structures except temporary variables.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Learning the Sorting Algorithm - Insertion Sort (Concepts)
Publish Date:2025/03/19 Views:95 Category:ALGORITHM
-
What is "insertion sort"? The concept is as follows: each time a record to be sorted is inserted into the previously sorted sequence according to its key size, until all records are inserted. Concepts are always somewhat abstract, and can a
Learning path of sorting algorithm - direct insertion sort
Publish Date:2025/03/19 Views:175 Category:ALGORITHM
-
This article follows up on Insertion Sort (Concepts) and presents the implementation steps and code for direct insertion sort. Since the Concepts section already has a large number of illustrations, it would be a bit long-winded to provide
Learning the sorting algorithm - Binary Insertion Sort
Publish Date:2025/03/19 Views:142 Category:ALGORITHM
-
This article follows the insertion sort (concept article) and presents the implementation steps and implementation code of the binary insertion sort Binary Insertion Sort Algorithm Steps Treat the first element of the first sequence to be s
The road to learning sorting algorithms - table insertion sort
Publish Date:2025/03/19 Views:193 Category:ALGORITHM
-
Table insertion sort was briefly mentioned in Insertion sort (concept) . I briefly summarized it and wrote this article. You can refer to it if you need it. Table insertion sort, as the name implies, uses an index table to sort the original
The road to learning sorting algorithms - Hill sort
Publish Date:2025/03/19 Views:50 Category:ALGORITHM
-
Hill sort is named after the designer of the algorithm, Hill. It is an improvement of Hill on the basis of insertion sort and can be said to be a special insertion sort. Here are the properties of insertion sort: First of all, the insertion
Things about the singleton design pattern
Publish Date:2025/03/19 Views:51 Category:ALGORITHM
-
The singleton design pattern is one of the most commonly used design patterns. The singleton design pattern, just by its name, you can roughly know its meaning. Single means one; instance means instance object. So a singleton has only one i
The road to learning sorting algorithms - merge sort
Publish Date:2025/03/19 Views:157 Category:ALGORITHM
-
Let's first look at the definition of merge sort Merge sort is an effective sorting algorithm based on the merge operation. This algorithm is a very typical application of the Divide and Conquer method. Merge the ordered subsequences to obt
The road to learning sorting algorithms - merge sort (non-recursive implementatio
Publish Date:2025/03/19 Views:188 Category:ALGORITHM
-
In the article "Merge Sort", we introduced the principles and operation steps of merge sort, and finally implemented the sorting algorithm using PHP code. In the program, we used the principle of recursion to implement the algorithm. In fac
The road to learning sorting algorithms - quick sort
Publish Date:2025/03/19 Views:121 Category:ALGORITHM
-
Quick sort is a sorting algorithm developed by Tony Hall. On average, sorting n items requires O(n log n) comparisons. In the worst case, O(n2) comparisons are required, but this is uncommon. In fact, quick sort is often significantly faste