Jump Search Algorithm

Jump Search is a searching algorithm that works on sorted arrays. It finds the target value by jumping ahead by fixed steps or block sizes to determine the search space efficiently. It's especially useful when performing searches in large arrays.

How Jump Search Works

  1. Initialization:

    • Determine the block size to jump ahead. Typically, it's set to the square root of the array's length.
  2. Jumping Blocks:

    • Jump ahead in fixed steps until the block containing the target value is found.
  3. Linear Search in Block:

    • Perform a linear search within the identified block to find the target value.
  4. Completion:

    • Continue this process until the target value is found or the end of the array is reached.

Key Features

  • Efficiency: Jump Search is more efficient than linear search on large arrays.

  • Optimal Block Size: The optimal block size can be calculated based on the array's size for better efficiency.

Efficiency

  • Time Complexity: Jump Search has a time complexity of O(√n), where n is the number of elements in the array.

  • Space Complexity: Jump Search has a space complexity of O(1) as it doesn't require extra space.

Applications

  • Large Arrays: It is especially useful for searching in large arrays where other search algorithms like linear search might be inefficient.

Advantages

  • Jump Search is efficient on large arrays.
  • It is simple to implement and doesn't require extra space.
  • It can be combined with other search algorithms to improve overall efficiency.

Disadvantages

  • It requires the array to be sorted.
  • It might not perform as well as some other search algorithms on smaller arrays.

Jump Search Implementation in JavaScript

Here's an example of Jump Search implemented in JavaScript:

Sorting-Algo/jmps.js
function jumpSearch(arr, target) {
    const n = arr.length;
    let step = Math.floor(Math.sqrt(n));
    let prev = 0;
 
    while (arr[Math.min(step, n) - 1] < target) {
        prev = step;
        step += Math.floor(Math.sqrt(n));
        if (prev >= n) return -1;
    }
 
    while (arr[prev] < target) {
        prev++;
        if (prev === Math.min(step, n)) return -1;
    }
 
    if (arr[prev] === target) return prev;
 
    return -1;
}
 
const arr = [1, 3, 5, 7, 9, 11, 13, 15, 17];
const target = 13;
const result = jumpSearch(arr, target);
console.log(`Target ${target} found at index ${result}`);