VB .NET Program to Implement Jump Search

VB .NET Program to Implement Jump Search

Searching efficiently in a list of data is a core part of programming, especially when the dataset is large and sorted. While Binary Search is widely used, Jump Search offers another efficient method for sorted arrays. Jump Search works by dividing the list into blocks of a fixed size and jumping ahead by these blocks until it finds the block that may contain the target. Once it identifies the block, it performs a linear search within that block to find the exact position of the element.

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 particularly useful when working with large datasets stored in memory or in cases where accessing elements has a fixed cost per block. By reducing the number of comparisons compared to a pure linear search, it provides a practical balance between simplicity and efficiency. Beginners can learn how block-wise searching works and how it improves search performance over linear methods.

Program 1: Iterative Jump Search

This program demonstrates Jump Search using a simple loop, searching for a number in a sorted array of integers.

Module Module1

    Sub Main()

        Dim data() As Integer = {1, 3, 5, 7, 9, 11, 13, 15, 17}
        Dim target As Integer = 11
        Dim n As Integer = data.Length
        Dim stepSize As Integer = Math.Floor(Math.Sqrt(n))
        Dim prev As Integer = 0

        While data(Math.Min(stepSize, n) - 1) < target

            prev = stepSize
            stepSize += Math.Floor(Math.Sqrt(n))

            If prev >= n Then

                Console.WriteLine("Element not found in the array.")
                Exit Sub

            End If

        End While

        For i As Integer = prev To Math.Min(stepSize, n) - 1

            If data(i) = target Then

                Console.WriteLine("Element found at position: " & i)
                Exit Sub

            End If

        Next

        Console.WriteLine("Element not found in the array.")
        Console.ReadLine()

    End Sub

End Module

The program calculates the optimal block size as the square root of the array’s length. It jumps ahead by this block size until it finds the block containing the target, then searches linearly inside the block. Beginners can see how Jump Search balances block jumps and linear checks to reduce comparisons.

Program 2: Jump Search with Negative Numbers

This version demonstrates that Jump Search works with negative numbers, as long as the array is sorted.

Module Module1

    Sub Main()

        Dim data() As Integer = {-20, -15, -10, -5, 0, 5, 10, 15}
        Dim target As Integer = -10
        Dim n As Integer = data.Length
        Dim stepSize As Integer = Math.Floor(Math.Sqrt(n))
        Dim prev As Integer = 0

        While data(Math.Min(stepSize, n) - 1) < target

            prev = stepSize
            stepSize += Math.Floor(Math.Sqrt(n))

            If prev >= n Then

                Console.WriteLine("Element not found in the array.")
                Exit Sub

            End If

        End While

        For i As Integer = prev To Math.Min(stepSize, n) - 1

            If data(i) = target Then

                Console.WriteLine("Element found at position: " & i)
                Exit Sub

            End If

        Next

        Console.WriteLine("Element not found in the array.")
        Console.ReadLine()

    End Sub

End Module

The algorithm only depends on comparisons and does not require positive numbers. Beginners can see that sorting is the key requirement, and Jump Search works with negative values seamlessly.

Program 3: Jump Search for Floating-Point Numbers

Jump Search can also be applied to decimal values in sorted arrays.

Module Module1

    Sub Main()

        Dim data() As Double = {0.5, 1.0, 1.5, 2.0, 2.5, 3.0}
        Dim target As Double = 2.0
        Dim n As Integer = data.Length
        Dim stepSize As Integer = Math.Floor(Math.Sqrt(n))
        Dim prev As Integer = 0

        While data(Math.Min(stepSize, n) - 1) < target

            prev = stepSize
            stepSize += Math.Floor(Math.Sqrt(n))

            If prev >= n Then

                Console.WriteLine("Element not found in the array.")
                Exit Sub

            End If

        End While

        For i As Integer = prev To Math.Min(stepSize, n) - 1

            If data(i) = target Then

                Console.WriteLine("Element found at position: " & i)
                Exit Sub

            End If

        Next

        Console.WriteLine("Element not found in the array.")
        Console.ReadLine()

    End Sub

End Module

Floating-point comparisons work just like integer comparisons. Beginners can experiment with decimals to see that Jump Search adapts to different numeric types while maintaining the same block-wise logic.

Program 4: Recursive Jump Search

This program implements Jump Search using recursion, highlighting an alternative way to structure the algorithm.

Module Module1

    Function JumpSearch(arr() As Integer, target As Integer, prev As Integer, stepSize As Integer) As Integer

        Dim n As Integer = arr.Length

        If prev >= n Then Return -1

        If arr(Math.Min(stepSize, n) - 1) >= target Then

            For i As Integer = prev To Math.Min(stepSize, n) - 1
                If arr(i) = target Then Return i
            Next

            Return -1

        End If

        Return JumpSearch(arr, target, stepSize, stepSize + Math.Floor(Math.Sqrt(n)))

    End Function

    Sub Main()

        Dim data() As Integer = {2, 4, 6, 8, 10, 12, 14}
        Dim target As Integer = 10
        Dim result As Integer = JumpSearch(data, target, 0, Math.Floor(Math.Sqrt(data.Length)))

        If result <> -1 Then
            Console.WriteLine("Element found at position: " & result)
        Else
            Console.WriteLine("Element not found in the array.")
        End If

        Console.ReadLine()

    End Sub

End Module

The recursive approach divides the array into blocks and calls itself until the block containing the target is found. Beginners can see how recursion mirrors iteration and helps practice thinking in terms of repeated subproblems.

Program 5: Interactive Jump Search

This program lets users enter a sorted array and target number, demonstrating a real-world application.

Module Module1

    Sub Main()

        Console.WriteLine("Enter sorted numbers separated by space:")

        Dim input As String = Console.ReadLine()
        Dim data() As Integer = Array.ConvertAll(input.Split(" "c), Function(s) Int32.Parse(s))

        Console.WriteLine("Enter number to search:")

        Dim target As Integer = Int32.Parse(Console.ReadLine())

        Dim n As Integer = data.Length
        Dim stepSize As Integer = Math.Floor(Math.Sqrt(n))
        Dim prev As Integer = 0
        Dim found As Boolean = False

        While prev < n AndAlso data(Math.Min(stepSize, n) - 1) < target

            prev = stepSize
            stepSize += Math.Floor(Math.Sqrt(n))

        End While

        For i As Integer = prev To Math.Min(stepSize, n) - 1

            If data(i) = target Then

                Console.WriteLine("Element found at position: " & i)
                found = True

                Exit For

            End If

        Next

        If Not found Then Console.WriteLine("Element not found in the array.")

        Console.ReadLine()

    End Sub

End Module

This interactive version shows how Jump Search can be applied in dynamic scenarios. Beginners can experiment with different inputs and see the efficiency gain over linear search in large datasets.

Frequently Asked Questions (FAQ)

Here are some common questions beginners ask about Jump Search:

Q1: What is the main advantage of Jump Search?
It reduces comparisons by jumping through blocks instead of checking every element, which is faster than linear search for large, sorted arrays.

Q2: Can Jump Search work with negative numbers or decimals?
Yes. As long as the array is sorted, it works with both negative and floating-point numbers.

Q3: How is the block size determined?
The optimal block size is typically the square root of the array’s length, balancing jumps and linear search inside blocks.

Q4: Does the array need to be sorted?
Yes. Jump Search relies on sorted data to work correctly.

Q5: Is Jump Search faster than Binary Search?
It can be faster for smaller datasets or when block jumps are cheaper, but Binary Search is generally more efficient for very large arrays.

Conclusion

Jump Search is a practical and efficient searching algorithm for sorted arrays. By dividing data into blocks and jumping ahead, it reduces unnecessary comparisons while keeping the logic simple. Beginners can explore iterative, recursive, interactive, negative numbers, and floating-point examples to understand its versatility. Practicing Jump Search strengthens algorithmic thinking and helps programmers select the right strategy depending on the type of data in VB .NET applications.

Scroll to Top