Lua Program to Implement Linear Search

Lua Program to Implement Linear Search

Linear Search is one of the simplest and most beginner-friendly search algorithms in programming. The idea behind it is straightforward: to find a particular value in a list or array, we check each element one by one until we find a match or reach the end. This simplicity makes Linear Search a great starting point for learning how search operations work in programming.

Although Linear Search is not the fastest method for large datasets, it is still widely used because of its simplicity and flexibility. It works on any type of data, whether the list is sorted or not, and can handle numbers, strings, or even complex objects. Learning Linear Search in Lua helps beginners understand array traversal, basic conditional checks, and loops, which are foundational skills for any programmer.

Program 1: Basic Linear Search using a for loop

This first program demonstrates the simplest form of Linear Search using a for loop. It searches for a number in a predefined array.

-- Basic Linear Search in Lua

function linearSearch(arr, target)

    for i = 1, #arr do

        if arr[i] == target then
            return i  -- Return the index if found
        end

    end

    return -1  -- Return -1 if not found

end

local numbers = {5, 3, 8, 1, 4}
local target = 8
local result = linearSearch(numbers, target)

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

This program works by checking each element one by one using a for loop. When the target value is found, the function returns the index. Beginners can easily understand that Linear Search is a sequential process and a good introduction to array traversal.

Program 2: Linear Search with detailed output

In this version, the program prints a message for each comparison, helping beginners visualize how the search progresses.

-- Linear Search with detailed output

function linearSearch(arr, target)

    for i = 1, #arr do

        print("Checking element: " .. arr[i])

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

    end

    return -1

end

local numbers = {7, 2, 9, 4, 6}
local target = 4
local position = linearSearch(numbers, target)

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

By printing each comparison, beginners can follow the search step by step. This helps them understand that Linear Search evaluates each element until it either finds a match or reaches the end.

Program 3: Linear Search using recursion

This program demonstrates how Linear Search can also be implemented using recursion. This method is elegant and shows how recursion can replace loops.

-- Recursive Linear Search

function recursiveLinearSearch(arr, target, index)

    index = index or 1

    if index > #arr then
        return -1
    end

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

    return recursiveLinearSearch(arr, target, index + 1)

end

local numbers = {10, 20, 30, 40, 50}
local target = 30
local result = recursiveLinearSearch(numbers, target)

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

Recursion allows the search function to call itself for the next index until it finds the target. Beginners can learn how recursion can simplify repetitive tasks like checking each element in an array.

Program 4: Linear Search for strings

Linear Search works not only for numbers but also for strings. This program searches for a specific string in an array of words.

-- Linear Search for strings

function linearSearch(arr, target)

    for i = 1, #arr do

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

    end

    return -1

end

local words = {"apple", "banana", "cherry", "date"}
local target = "cherry"
local result = linearSearch(words, target)

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

This example shows that Linear Search is flexible. Beginners can see that the same logic works for strings, highlighting the algorithm’s versatility across different types of data.

Program 5: Linear Search handling negative and floating-point numbers

Linear Search works naturally with negative and floating-point numbers. This program demonstrates a search with mixed numeric types.

-- Linear Search for negative and floating-point numbers

function linearSearch(arr, target)

    for i = 1, #arr do

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

    end

    return -1

end

local numbers = {-5, 3.2, -1.5, 7.8, 0}
local target = -1.5
local result = linearSearch(numbers, target)

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

This demonstrates that Linear Search does not rely on ordering or specific types. Beginners learn that it is a general-purpose search method that works for a wide range of numeric data.

Frequently Asked Questions (FAQ)

This section answers common questions beginners may have about Linear Search in Lua.

Q1. Is Linear Search efficient for large datasets?
Linear Search is simple but not very efficient for large datasets because it checks every element one by one.

Q2. Can Linear Search handle unsorted arrays?
Yes, Linear Search works on both sorted and unsorted arrays.

Q3. Can Linear Search be used for strings?
Yes, Linear Search can search for strings, numbers, or any comparable elements.

Q4. Is recursion better than loops for Linear Search?
Recursion is elegant, but for large arrays, loops are more efficient and avoid stack overflow.

Q5. Does Linear Search require extra memory?
No, Linear Search works in-place and does not need additional memory beyond variables for indexing.

Conclusion

Linear Search is a foundational algorithm that introduces beginners to searching in arrays. Through the examples in this article, you learned how to implement it using loops, recursion, strings, and even negative or floating-point numbers. The simplicity and flexibility of Linear Search make it a great starting point for understanding more advanced search algorithms in the future. Practicing these examples in Lua will strengthen your skills and provide a solid foundation for exploring more complex algorithms confidently.

Scroll to Top