Day 9: Jump Search (Square Root Search)

1. Introduction

Jump Search is a searching algorithm for sorted arrays.
It works by jumping ahead by fixed steps (instead of checking every element like Linear Search) and then doing a Linear Search in a smaller block where the target might be.

It’s often faster than Linear Search but slower than Binary Search — and it’s useful when:

  • You want a balance between simplicity and performance.

  • Random access (direct index access) is possible.


2. How It Works

  1. Determine a block size (commonly √n, where n is the number of elements).

  2. Jump forward in steps of this block size until you either find the target or pass it.

  3. Perform a Linear Search within the block where the target might be.


Example:
Array = [1, 3, 5, 7, 9, 11, 13, 15], target = 11

  • Step size = √8 ≈ 2

  • Check index 0 → 1

  • Jump to index 2 → 5

  • Jump to index 4 → 9

  • Jump to index 6 → 13 (passed target)

  • Linear search from index 4 to 6 → found 11.


3. Pseudocode

arduinoCopyEditfunction jumpSearch(arr, target):
    n = length of arr
    step = floor(sqrt(n))
    prev = 0

    while arr[min(step, n)-1] < target:
        prev = step
        step += floor(sqrt(n))
        if prev >= n:
            return -1

    while arr[prev] < target:
        prev++
        if prev == min(step, n):
            return -1

    if arr[prev] == target:
        return prev

    return -1

4. JavaScript Implementation

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

    return arr[prev] === target ? prev : -1;
}

// Example usage
const arr = [1, 3, 5, 7, 9, 11, 13, 15];
console.log(jumpSearch(arr, 11)); // Output: 5

5. Time Complexity

  • Best case: O(1)

  • Average case: O(√n)

  • Worst case: O(√n)
    Space complexity: O(1) (in-place)


6. When to Use

  • Works best when:

    • The array is sorted.

    • Random access to elements is possible (e.g., arrays, not linked lists).

    • You need faster performance than Linear Search but Binary Search isn’t possible (due to some constraints).

#CodeWithGift #DSA2025 #JavaScript #DayNine

1
Subscribe to my newsletter

Read articles from God'sgift Samuel directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

God'sgift Samuel
God'sgift Samuel

About Me Tech enthusiast and developing web developer with a growing passion for blockchain technology. My journey spans 3 years of HTML/CSS crafting, 2 years of JavaScript exploration, and recent ventures into React, Node.js, and the blockchain space. Currently at the beginning of my blockchain journey while building a solid foundation in web development technologies. Fascinated by the potential of decentralized systems and eager to contribute to this evolving ecosystem. Technical Background I bring a diverse technical toolkit that includes: Strong foundation in web fundamentals (HTML/CSS: 3 years) Dynamic front-end development with JavaScript (2 years) and React (1 year) Modern UI implementation using Tailwind CSS and Bootstrap (7 months) Server-side programming with Node.js (1 year) and Python (2-3 years) Early-stage blockchain development knowledge Beginning exploration of Rust programming (4 months) Blockchain Journey While still at the beginner level in blockchain technology, I'm actively learning about distributed ledger concepts, smart contract fundamentals, and the broader implications of Web3. My interest in this space stems from a belief in the transformative potential of decentralized technologies and their ability to reshape digital interactions. Vision & Goals My development path is guided by a clear progression: mastering web development fundamentals, expanding into blockchain applications, and ultimately exploring the intersection of these technologies with artificial intelligence. I see tremendous potential in combining these domains to create innovative solutions for tomorrow's challenges. Collaboration Interests Open to connecting with fellow developers, blockchain enthusiasts, and mentors who share an interest in the convergence of web development and emerging technologies. Particularly interested in learning opportunities, knowledge exchange, and potential collaboration on projects that push the boundaries of what's possible in the decentralized space. Current Focus Deepening my understanding of React and Node.js ecosystems while simultaneously building knowledge in blockchain fundamentals and smart contract development. Committed to continuous learning and practical application of new skills.