JavaScript Program to Implement Jump Search

JavaScript Program to Implement Jump Search

Jump Search is a searching algorithm that sits somewhere between Linear Search and Binary Search. It works by dividing a sorted array into blocks and jumping ahead by a fixed number of elements, rather than checking each element one by one. Once the block that may contain the target element is found, a simple linear search is used within that block. This method can be faster than Linear Search, especially for large datasets, while still being simpler than Binary Search.

Pluralsight Logo
Accelerate your tech career
with hands-on learning.
Whether you're a tech newbie or a total pro,
get the skills and confidence to land your next move.
Start 10-Day Free Trial

Learning Jump Search is useful for beginners because it introduces the concept of block-wise searching and helps understand optimization techniques for searching algorithms. It is commonly used in applications where the dataset is sorted, and you want a compromise between the simplicity of Linear Search and the efficiency of Binary Search. Examples include searching in indexed files, small databases, or structured lists where jumping ahead saves time.

Program 1: Basic Jump Search

This program demonstrates the standard Jump Search algorithm using a fixed block size and a linear search within the block.

function jumpSearch(arr, target) {

    let n = arr.length;
    let step = Math.floor(Math.sqrt(n)); // Block size
    let prev = 0;

    while (arr[Math.min(step, n) - 1] < target) {
        prev = step;
        step += Math.floor(Math.sqrt(n));
        if (prev >= n) return -1;
    }

    for (let i = prev; i < Math.min(step, n); i++) {
        if (arr[i] === target) return i;
    }

    return -1;

}

let numbers = [1, 3, 5, 7, 9, 11, 13];
let target = 7;
let result = jumpSearch(numbers, target);

console.log(result !== -1 ? `Element found at index ${result}` : "Element not found");

This program works by jumping ahead in blocks and then performing a linear search in the identified block. Beginners can think of it as “skip ahead in steps, then check carefully inside the small section.” It is useful because it reduces the number of comparisons compared to a pure Linear Search, especially for larger arrays.

Program 2: Jump Search using while loop

This version uses a while loop for the block jumping, demonstrating an alternative approach to iteration.

function jumpSearchWhile(arr, target) {

    let n = arr.length;
    let step = Math.floor(Math.sqrt(n));
    let prev = 0;

    while (prev < n && arr[Math.min(step, n) - 1] < target) {
        prev = step;
        step += Math.floor(Math.sqrt(n));
    }

    let i = prev;

    while (i < Math.min(step, n)) {
        if (arr[i] === target) return i;
        i++;
    }

    return -1;

}

let values = [2, 4, 6, 8, 10, 12, 14];
let searchTarget = 10;
let resultWhile = jumpSearchWhile(values, searchTarget);

console.log(resultWhile !== -1 ? `Element found at index ${resultWhile}` : "Element not found");

Here, both block jumps and linear searches are handled using while loops. Beginners can understand it as “keep moving by blocks and then check each element in the block.” This approach can help learners see how loop control works in a more dynamic way.

Program 3: Jump Search Recursive

This example shows how Jump Search can be implemented recursively.

function jumpSearchRecursive(arr, target, start = 0, step = null) {

    let n = arr.length;
    if (step === null) step = Math.floor(Math.sqrt(n));

    if (start >= n || arr[start] > target) return -1;

    if (arr[Math.min(start + step, n) - 1] < target) {
        return jumpSearchRecursive(arr, target, start + step, step);
    }

    for (let i = start; i < Math.min(start + step, n); i++) {
        if (arr[i] === target) return i;
    }

    return -1;

}

let data = [5, 15, 25, 35, 45, 55, 65];
let searchValue = 35;
let resultRecursive = jumpSearchRecursive(data, searchValue);

console.log(resultRecursive !== -1 ? `Element found at index ${resultRecursive}` : "Element not found");

This recursive version works by “jumping” through blocks and calling the function again on the next block if the target is not in the current block. Beginners can see recursion as a way to break the problem into smaller, repeatable steps. It is useful for understanding how recursion can handle iterative processes.

Program 4: Jump Search for multiple occurrences

This program modifies Jump Search to find all occurrences of a target value in the array.

function jumpSearchAll(arr, target) {

    let n = arr.length;
    let step = Math.floor(Math.sqrt(n));
    let prev = 0;

    while (prev < n && arr[Math.min(step, n) - 1] < target) {
        prev = step;
        step += Math.floor(Math.sqrt(n));
    }

    // Find ONE match first
    let foundIndex = -1;

    for (let i = prev; i < Math.min(step, n); i++) {

        if (arr[i] === target) {
            foundIndex = i;
            break;
        }

    }

    if (foundIndex === -1) return [];

    let indices = [];

    // Go left
    let left = foundIndex;

    while (left >= 0 && arr[left] === target) {
        indices.push(left);
        left--;
    }

    // Go right
    let right = foundIndex + 1;

    while (right < n && arr[right] === target) {
        indices.push(right);
        right++;
    }

    return indices.sort((a, b) => a - b);

}

let numbersMultiple = [1, 3, 3, 3, 5, 7, 9];
let targetMultiple = 3;

let allResults = jumpSearchAll(numbersMultiple, targetMultiple);

console.log(
    allResults.length
        ? `Element found at indices: ${allResults}`
        : "Element not found"
);

This program works by storing all indices of the target found in the block. Beginners can understand it as “jump ahead, then check carefully and note every place it occurs.” It is useful when duplicates exist and all positions are needed.

Program 5: Jump Search as a reusable function

This program wraps Jump Search into a clean utility function suitable for multiple projects.

function jumpSearchUtility(arr, target) {

    let 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;
    }

    for (let i = prev; i < Math.min(step, n); i++) {
        if (arr[i] === target) return i;
    }

    return -1;

}

let dataset = [10, 20, 30, 40, 50, 60, 70];
let findValue = 40;
let resultUtility = jumpSearchUtility(dataset, findValue);

console.log(resultUtility !== -1 ? `Element found at index ${resultUtility}` : "Element not found");

This reusable function allows beginners to efficiently search sorted arrays using Jump Search. It demonstrates the importance of creating modular, practical code while optimizing searches.

Frequently Asked Questions (FAQ)

This section answers common beginner questions about Jump Search in JavaScript.

Q1. What is Jump Search in JavaScript?
Jump Search is an algorithm that searches a sorted array by jumping ahead in fixed-size blocks and then performing a linear search within the block.

Q2. Can Jump Search be used on unsorted arrays?
No, the array must be sorted for Jump Search to work correctly.

Q3. How is Jump Search different from Binary Search?
Jump Search jumps by fixed blocks instead of always dividing the array in half. It can be more efficient than Linear Search but simpler than Binary Search.

Q4. What is the time complexity of Jump Search?
The time complexity is O(√n), where n is the number of elements in the array.

Q5. Why should beginners learn Jump Search?
It introduces block-wise searching, optimization techniques, and provides a balance between simplicity and efficiency in searching algorithms.

Conclusion

Jump Search is a practical and efficient searching algorithm for sorted arrays. It introduces block-wise searching and combines the simplicity of Linear Search with partial optimization for faster performance.

By practicing iterative, recursive, multiple-occurrence, and reusable function implementations, beginners can gain a solid understanding of Jump Search. This strengthens their algorithmic thinking, helps handle large datasets more efficiently, and builds confidence in implementing optimized search strategies in JavaScript.

Scroll to Top