Lua Program to Implement Jump Search

Lua Program to Implement Jump Search

Jump Search is an efficient searching algorithm designed to find elements quickly in a sorted array. Unlike Linear Search, which checks each element one by one, Jump Search “jumps” ahead by fixed steps, reducing the number of comparisons. This makes it a great compromise between Linear Search and Binary Search, especially for datasets where random access is possible and the array is sorted. For beginners, it is a wonderful way to learn how algorithms can optimize searching without complex recursion.

In practice, Jump Search is useful for searching in large sorted datasets where the data is stored in arrays or lists, such as numeric IDs, timestamps, or grades. It is particularly helpful when each step in the array is expensive to access, as it minimizes the number of jumps before performing a final Linear Search in a small block. By learning Jump Search in Lua, you not only understand an efficient algorithm but also get hands-on practice with loops, arithmetic, and array manipulation.

Program 1: Iterative Jump Search

This first program demonstrates a basic iterative approach to Jump Search. It searches for a target number in a sorted array using a fixed step size.

-- Iterative Jump Search in Lua

function jumpSearch(arr, target)

    local n = #arr
    local step = math.floor(math.sqrt(n))
    local prev = 1

    while arr[math.min(step, n)] < target do

        prev = step + 1
        step = step + math.floor(math.sqrt(n))

        if prev > n then
            return -1
        end

    end

    for i = prev, math.min(step, n) do

        if arr[i] == target then
            return i
        end

    end

    return -1

end

local numbers = {1, 3, 5, 7, 9, 11, 13, 15}
local target = 9
local result = jumpSearch(numbers, target)

if result ~= -1 then
    print("Number found at position: " .. result)
else
    print("Number not found")
end

This program calculates a step size based on the square root of the array length. The algorithm jumps ahead in blocks and then performs a Linear Search within the identified block. Beginners can see how combining jumps and linear checking balances speed and simplicity.

Program 2: Jump Search with Recursion

This version demonstrates a recursive approach to Jump Search, narrowing down the search block using function calls.

-- Recursive Jump Search in Lua

function recursiveJumpSearch(arr, target, prev, step)

    prev = prev or 1
    step = step or math.floor(math.sqrt(#arr))

    if prev > #arr or arr[prev] > target then
        return -1
    end

    if arr[math.min(step, #arr)] < target then

        return recursiveJumpSearch(arr, target, step + 1, step + math.floor(math.sqrt(#arr)))

    else

        for i = prev, math.min(step, #arr) do

            if arr[i] == target then
                return i
            end

        end

    end

    return -1

end

local numbers = {2, 4, 6, 8, 10, 12, 14}
local target = 10
local result = recursiveJumpSearch(numbers, target)

if result ~= -1 then
    print("Number found at index: " .. result)
else
    print("Number not found")
end

Recursion allows the program to reduce the search space by jumping ahead and calling itself on smaller blocks. Beginners can understand this as a method of breaking a problem into smaller steps while still keeping the logic clear.

Program 3: Jump Search with Negative Numbers

Jump Search works well with negative numbers because comparisons are consistent across numeric ranges. This example shows searching a sorted array with negative and positive values.

-- Jump Search with negative numbers

function jumpSearch(arr, target)

    local n = #arr
    local step = math.floor(math.sqrt(n))
    local prev = 1

    while arr[math.min(step, n)] < target do

        prev = step + 1
        step = step + math.floor(math.sqrt(n))

        if prev > n then
            return -1
        end

    end

    for i = prev, math.min(step, n) do

        if arr[i] == target then
            return i
        end

    end

    return -1

end

local numbers = {-20, -10, 0, 10, 20, 30}
local target = -10
local result = jumpSearch(numbers, target)

if result ~= -1 then
    print("Number found at index: " .. result)
else
    print("Number not found")
end

Beginners can see that Jump Search doesn’t need any changes to handle negative values, highlighting its simplicity and versatility.

Program 4: Jump Search with Floating-Point Numbers

Jump Search can also be applied to arrays of floating-point numbers. This is useful for datasets where precision matters.

-- Jump Search with floating-point numbers

function jumpSearch(arr, target)

    local n = #arr
    local step = math.floor(math.sqrt(n))
    local prev = 1

    while arr[math.min(step, n)] < target do

        prev = step + 1
        step = step + math.floor(math.sqrt(n))

        if prev > n then
            return -1
        end

    end

    for i = prev, math.min(step, n) do

        if arr[i] == target then
            return i
        end

    end

    return -1

end

local numbers = {0.5, 1.5, 2.5, 3.5, 4.5, 5.5}
local target = 3.5
local result = jumpSearch(numbers, target)

if result ~= -1 then
    print("Number found at index: " .. result)
else
    print("Number not found")
end

This shows that Jump Search relies only on comparisons and is not limited to integers. Beginners can adapt this to any numeric dataset without changing the core logic.

Program 5: Reusable Jump Search Function

This final program wraps the iterative Jump Search into a reusable function suitable for integration into larger projects.

-- Reusable Jump Search Function

function jumpSearch(arr, target)

    local n = #arr
    local step = math.floor(math.sqrt(n))
    local prev = 1

    while arr[math.min(step, n)] < target do

        prev = step + 1
        step = step + math.floor(math.sqrt(n))

        if prev > n then
            return -1
        end

    end

    for i = prev, math.min(step, n) do

        if arr[i] == target then
            return i
        end

    end

    return -1

end

local numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
local target = 7
local index = jumpSearch(numbers, target)

if index ~= -1 then
    print("Target found at index: " .. index)
else
    print("Target not found")
end

Beginners can see how wrapping the logic in a function makes it easier to reuse and integrate into larger projects, while keeping the code clean and understandable.

Frequently Asked Questions (FAQ)

Jump Search can bring up several questions for beginners learning searching algorithms:

Q1. Does Jump Search work only on sorted arrays?
Yes, Jump Search requires the array to be sorted for correct results.

Q2. Is Jump Search faster than Linear Search?
Yes, Jump Search is faster because it reduces the number of comparisons by jumping ahead in blocks.

Q3. Can Jump Search handle negative numbers and floating-point numbers?
Absolutely. Jump Search works with any numeric type as long as the array is sorted.

Q4. How do I choose the step size?
The optimal step size is usually the square root of the array length, which balances jumps and linear checks.

Q5. When should I prefer Jump Search over Binary Search?
Jump Search is useful when array elements are uniformly distributed and you want a simpler alternative to recursion-based Binary Search.

Conclusion

Jump Search is an efficient and beginner-friendly algorithm for searching in sorted arrays. In this article, you learned how to implement Jump Search iteratively, recursively, and for arrays with negative and floating-point numbers. By practicing these programs in Lua, beginners can understand how block-based searching reduces comparisons and speeds up searches. Jump Search also teaches the importance of choosing the right algorithm for the dataset, helping you develop stronger problem-solving and programming skills.

Scroll to Top