C# Program to Implement Jump Search

C# Program to Implement Jump Search

Searching efficiently in a sorted list is one of the key skills for any programmer. While Linear Search checks each element one by one and Binary Search divides the array in half, Jump Search takes a middle path. It jumps ahead by fixed steps to quickly locate a block where the target element could exist, and then performs a linear search within that block. This combination makes Jump Search faster than Linear Search for large arrays while still being simple to understand.

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

Jump Search is especially useful when working with large, sorted arrays where accessing elements is costly, like in databases or memory-mapped files. It helps beginners understand trade-offs between jumping and linear checking, and why algorithm design is about balancing efficiency and simplicity. By learning Jump Search, you also develop intuition for other hybrid search algorithms that combine multiple strategies.

Program 1: Jump Search Using Loop

This program demonstrates the basic Jump Search algorithm using a simple loop. It jumps in steps, finds the right block, and then searches linearly within that block.

using System;

class Program
{

    static int JumpSearch(int[] arr, int key)
    {

        int n = arr.Length;
        int step = (int)Math.Floor(Math.Sqrt(n));
        int prev = 0;

        while (arr[Math.Min(step, n) - 1] < key)
        {

            prev = step;
            step += (int)Math.Floor(Math.Sqrt(n));

            if (prev >= n)
                return -1;

        }

        for (int i = prev; i < Math.Min(step, n); i++)
        {

            if (arr[i] == key)
                return i;

        }

        return -1;

    }

    static void Main()
    {

        int[] numbers = { 1, 3, 5, 7, 9, 11, 13, 15 };
        int target = 9;

        int result = JumpSearch(numbers, target);

        if (result != -1)
            Console.WriteLine($"Element {target} found at index {result}");
        else
            Console.WriteLine($"Element {target} not found in the array");

    }

}

This loop-based program is easy for beginners to follow. It shows how jumping by steps reduces the number of comparisons compared to checking each element individually.

Program 2: Jump Search Using Method

Here, we encapsulate the logic into a reusable method. This approach improves code readability and makes the search function easy to reuse in other programs.

using System;

class Program
{

    static int JumpSearchMethod(int[] arr, int key)
    {

        int n = arr.Length;
        int step = (int)Math.Sqrt(n);
        int prev = 0;

        while (prev < n && arr[Math.Min(step, n) - 1] < key)
        {
            prev = step;
            step += (int)Math.Sqrt(n);
        }

        for (int i = prev; i < Math.Min(step, n); i++)
        {

            if (arr[i] == key)
                return i;

        }

        return -1;

    }

    static void Main()
    {

        int[] numbers = { 2, 4, 6, 8, 10, 12, 14 };
        int target = 12;

        int result = JumpSearchMethod(numbers, target);

        if (result != -1)
            Console.WriteLine($"Element {target} found at index {result}");
        else
            Console.WriteLine($"Element {target} not found in the array");

    }

}

Using a method makes it easier to understand and maintain. Beginners can call this method with different arrays and targets without rewriting the algorithm each time.

Program 3: Jump Search with Recursion

Jump Search can also be implemented recursively. The recursive approach splits the array into blocks and checks them until the target is found.

using System;

class Program
{

    static int JumpSearchRecursive(int[] arr, int key, int start, int step)
    {

        int n = arr.Length;

        if (start >= n)
            return -1;

        if (arr[Math.Min(step, n) - 1] < key)
            return JumpSearchRecursive(arr, key, step, step + (int)Math.Sqrt(n));

        for (int i = start; i < Math.Min(step, n); i++)
        {

            if (arr[i] == key)
                return i;

        }

        return -1;

    }

    static void Main()
    {

        int[] numbers = { 5, 10, 15, 20, 25, 30, 35 };
        int target = 25;

        int step = (int)Math.Sqrt(numbers.Length);
        int result = JumpSearchRecursive(numbers, target, 0, step);

        if (result != -1)
            Console.WriteLine($"Element {target} found at index {result}");
        else
            Console.WriteLine($"Element {target} not found in the array");

    }

}

Recursion helps beginners understand breaking problems into smaller blocks. It also demonstrates how Jump Search naturally divides arrays into manageable segments.

Program 4: Jump Search for String IDs

Jump Search is versatile and can be applied to numeric strings, like IDs, by converting them into integers.

using System;

class Program
{

    static int JumpSearchString(string[] arr, string key)
    {

        int[] intArr = Array.ConvertAll(arr, int.Parse);
        int keyInt = int.Parse(key);
        int n = intArr.Length;
        int step = (int)Math.Sqrt(n);
        int prev = 0;

        while (prev < n && intArr[Math.Min(step, n) - 1] < keyInt)
        {

            prev = step;
            step += (int) Math.Sqrt(n);

        }

        for (int i = prev; i < Math.Min(step, n); i++)
        {

            if (intArr[i] == keyInt)
                return i;

        }

        return -1;

    }

    static void Main()
    {

        string[] ids = { "100", "200", "300", "400", "500" };
        string target = "300";

        int result = JumpSearchString(ids, target);

        if (result != -1)
            Console.WriteLine($"ID {target} found at index {result}");
        else
            Console.WriteLine($"ID {target} not found in the array");

    }

}

This program shows that Jump Search can handle sorted string data representing numbers. It teaches beginners how to adapt algorithms for slightly different data types.

Frequently Asked Questions (FAQ)

Q1: When is Jump Search better than Linear Search?
Jump Search is faster for large sorted arrays because it skips blocks instead of checking every element.

Q2: Can Jump Search be used for unsorted arrays?
No. Jump Search only works on sorted arrays.

Q3: What is the time complexity of Jump Search?
Its average and worst-case time complexity is O(√n), making it faster than Linear Search for large arrays.

Q4: How do you choose the jump size?
The optimal jump size is √n, where n is the number of elements in the array.

Conclusion

Jump Search is a simple yet efficient search algorithm for sorted arrays. It teaches beginners how to balance skipping and linear checking for faster results. By practicing Jump Search with loops, recursion, and different data types, you develop a solid foundation in search algorithms.

Once you are comfortable with Jump Search, you can explore Interpolation Search, Exponential Search, and Fibonacci Search, which provide more ways to optimize searching in real-world applications.

Scroll to Top