Jump Search Algorithm

Distribute Education like Computer Geek

Today, we’re going to explore the “Jump Search” algorithm, a powerful technique used in the field of Design and Analysis of Algorithms. Don’t worry if you’re new to this, we’ll break it down in simple terms.

.

Introduction

Jump Search is like a treasure hunt. Imagine you’re searching for a specific item in a large pile, but instead of checking every item one by one, you take giant leaps and narrow down your search. It’s an efficient way to find what you’re looking for, and that’s what Jump Search does with data.

Jump search was invented by William Kellogg and Kenneth Patz in 2002.

.

What is a Jump Search

  1. Jump Search is a searching algorithm used to locate a specific element within a sorted array or list by making a series of jumps or steps, which allows for a more efficient search process.
  2. If array is sorted, then we can search the target element first at b location, then 2b location and then 3b, 4b… up to the array ends.
  3. At any moment, if target element is larger than the array element, then search at most b more elements in gap.
  4. Time Complexity: n/b + b where n is numbers in array.
  5. This process continues until the target element is found or it is determined that the element does not exist in the array.
How b is calculated?

If n = 100

b   | n/b + b
-------------
2 | 52
5 | 25
10 | 20 (min)
15 | 22
20 | 25
50 | 52

So, in b = 10, there is the smallest comparison in the jump search, it means for n = 100, b = √n = 10

There is another method to find b which gives correct result.

To find smallest b, we can differentiate (n/b + n) with respect to b.

Differentiate (n/b + b) = 0
-> d/db (n/b + b) = 0
-> -n/b2 + 1 = 0
-> b2 = n
-> b = √n

Number of operations when b = √n

So, (n/b + b) = (n/√n + √n) = 2√n

It means, Ɵ(√n) is time complexity.

.

How Jump Search Works

Sorting– First, your data must be sorted in ascending order. This is essential for Jump Search to work effectively.

Setting the Jump– Determine a “jump size” or “step.” This step defines how far you’ll leap through the data while searching. A common choice is the square root of the data size.

Jumping– Start with the first element and jump ahead in fixed steps. If the array element is greater than the target element, then backtrack (last element which is less than the target element) and perform a linear search.

Repeat Until Found– Keep jumping and searching until you find the desired element or determine that it’s not in the data.

.

The Jump Search Algorithm

1. Let 'array' be the sorted array to search.
2. Let 'n' be the size of the array.
3. Let 'step' be the jump size (often sqrt(n)).
4. Initialize 'prev' to 0.
5. While 'array[min(step, n) - 1]' < 'target':
- Set 'prev' to 'step'.
- Increment 'step' by 'sqrt(n)'.
- If 'prev' >= 'n'
the target is not in the array; stop.
6. Perform a linear search in the range 'prev'
to 'min(step, n)' for the 'target'.
7. If found, return the index;
otherwise, return -1 (not found).

.

Example

Let’s say you have a sorted array

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16],

and you want to find the number 10.

The jump size would be sqrt(16) = 4.

Jump Search Algorithm

.

Start at index 4, (4 < 10), Comparison = 1

So, jump to value 8. (8 < 10), Comparison = 2

Jump Search Algorithm

.

So, jump to value 12. (12 > 10), Comparison = 3

Jump Search Algorithm

.

Since 12 is greater than 10, you backtrack to value 8 and perform a linear search.

You find value 9. (9 < 10), Comparison = 4

Jump Search Algorithm

.

So, count +1 for 9, the value is 10 (10 = 10). Comparison = 5

Search successful.

Search Succesful

 

Source Code of Jump Search

Time and Space Complexity
  1. Best case Jump Search performs at most 1 comparison because target element = square root of n.
  2. Average and Worst-case Jump search performs O(√n) comparison.
  3. Space Complexity– O(1) – Jump Search uses a constant amount of extra space for variables, regardless of the input size.

 

Advantages of Jump Search
  1. Efficient for searching in sorted arrays.
  2. Requires less time compared to linear search, especially for large datasets.
  3. It’s simple to implement.

 

Disadvantages of Jump Search
  1. Requires sorted data, which can be a limitation.
  2. May perform poorly for datasets with many repeated values.
  3. It’s not suitable for unsorted data.

 

Applications of Jump Search
  1. Searching in databases and indexing systems.
  2. File systems for quick file retrieval.
  3. Wherever a sorted dataset needs efficient search operations.

Test Yourself

Q1- Explain the concept of Jump Search in algorithms.

Jump Search is a searching algorithm used to locate a specific element within a sorted array or list by making a series of jumps or steps, allowing for an efficient search process. It works by dividing the data into blocks and performing a jump to narrow down the search.

Q2- What is the significance of sorting data before using the Jump Search algorithm?

Sorting the data is essential for Jump Search because it relies on the data being in ascending order. Without sorting, Jump Search cannot efficiently locate the target element.

Q3- How is the “jump size” or “step” determined in the Jump Search algorithm?

The jump size, often denoted as ‘b,’ is typically set to the square root of the size of the data (sqrt(n)).

It defines how far you jump through the data while searching.

Q4- Explain the process of jumping and searching in the Jump Search algorithm.

Jump Search starts with the first element and jumps ahead in fixed steps (determined by the jump size ‘b’).

It compares each element with the target.

If the element is greater than the target, it backtracks and performs a linear search within the block.

Q5- How is the time complexity of Jump Search calculated, and what is it for this algorithm?

We have number of operations (n/b + n)

When b = √n

=> (n/b + b) = (n/√n + √n) = 2√n

=> Ɵ(√n) is time complexity.

Q6- How to find the smallest b?

To find smallest b, we can differentiate (n/b + n) with respect to b.

Differentiate (n/b + b) = 0
-> d/db (n/b + b) = 0
-> -n/b2 + 1 = 0
-> b2 = n
-> b = √n
Q7- What are the advantages of using Jump Search for searching in sorted arrays?

Advantages of Jump Search include efficiency for large datasets, simplicity of implementation, and a time complexity of O(√n) for average and worst cases.

Q8- In the worst-case scenario, how many comparisons are performed in Jump Search for an array of size ‘n’?

In the worst case, Jump Search performs approximately 2√n comparisons for an array of size ‘n.’

Q9- What is the time complexity of the Jump Search algorithm?
  1. O(log n)
  2. O(n)
  3. O(√n)
  4. O(n log n)

Ans – (3)

Explanation – Jump Search has a time complexity of O(√n) for average and worst cases.

Q10- When performing a jump in Jump Search, what does the algorithm do if the array element is greater than the target?
  1. Continues jumping forward
  2. Performs a linear search within the block
  3. Halts the search
  4. Increases the jump size

Ans – (2)

Explanation – It backtracks and performs a linear search when encountering a larger element.

Q11- Is Jump Search always faster than Linear Search for sorted arrays?

Not necessarily.

While Jump Search is generally faster than Linear Search for large sorted arrays due to its O(√n) time complexity, it can actually be slower than Linear Search for very small arrays or when the target element is located near the beginning of the array. In such cases, the overhead of setting up the jump size may make Linear Search more efficient.

Q12- What is the minimum size an array should have for Jump Search to be more efficient than Binary Search?

Jump Search is typically more efficient than Binary Search for very small arrays (e.g., less than 10-20 elements) due to its lower overhead. However, as the array size increases, Binary Search eventually surpasses Jump Search in terms of efficiency.

BOOKS

Algorithm T H CoremanAlgorithm by Udit AgarwalAlgorithm Ellis HorowitzData Structure & Algorithm Made Easy Narasimha

Â