linear search Algorithm

The linear search algorithm, also known as the sequential search algorithm, is a fundamental and straightforward method for searching an element in a list or an array. It works by iterating through each element in the list or array one-by-one, comparing the target value to each element until a match is found or the end of the list is reached. Linear search is considered a basic algorithm because of its simplicity and ease of implementation. It does not require any advanced data structures or pre-processing steps, making it universally applicable to any type of list or array. However, the primary disadvantage of the linear search algorithm is its inefficiency, particularly in large datasets. The algorithm has a worst-case time complexity of O(n), meaning that its execution time increases linearly with the size of the input data. In the worst case, the target element could be the last element in the list or not present at all, requiring a full traversal of the entire list. This can be problematic when dealing with substantial amounts of data or when the search operation needs to be performed frequently. In such cases, more advanced search algorithms, such as binary search, interpolation search, or hashing techniques, are often more suitable alternatives.
function p = linear_search(A,t)
%% linear Search
% This function linear searches target value (t) in array A. 
% For this, It sequentially checks each entry of the array until a match is found
% or  or the whole list has been searched.
% If it can find the target returns 1 otherwise 0. 

array_length = length(A);
i = 1;
searchTermination = 0;

while searchTermination == 0 && i < array_length+1
    if A(i) == t
        p = 1;
        searchTermination = 1;
        disp('the target is found in the array')
    else
        i = i+1;
    end
end
if i == array_length+1
    p = 0;
    disp('the target is not found in the array')
end
end

LANGUAGE:

DARK MODE: