PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience

Jul 02 2018, 5:41 PM PST 2 min

The Linear Search is a Data Searching algorithm. Linear search is the simplest searching algorithm which uses a brute-force method of checking every element in the list in order until the proper result is found.

Procedure

  1. Check if current element is equivalent to search query
    • If it is, return current element position and end search
    • If not, do nothing
  2. Go on to next element and repeat

Example Code

function linear_search(tab, query)
    --Go through every position in the table
    for i, v in ipairs(tab) do
        --if the current element is the query
        if v == query then
            --return its position
            return i
        end
    end
end
Tags:
  • search