Linear Search Algorithm

Linear Search Definition

Linear search also called sequential search is a type of search algorithms, that traverse an array and compare each item with the wanted item, if the item is found the algorithm returns his index otherwise, It return…


This content originally appeared on DEV Community and was authored by Aya Bouchiha

Linear Search Definition

Linear search also called sequential search is a type of search algorithms, that traverse an array and compare each item with the wanted item, if the item is found the algorithm returns his index otherwise, It returns a false value (false, null, None,0...)

Space and Time complexity of linear search

The time complexity of linear search is O(n) and his Space complexity is O(1)

line-graph

Implementaion of linear search in python

def LinearSearchAlgorithm(wantedItem,items: list):
    """
        Linear seach algorithm
        input: 
            [wantedItem]
            [items] {list}
        output:
            => returns index if the item is found
            => returns False if the item is not found
    """
    for i in range(len(items)):
        if wantedItem == items[i]:
            return i
    return False

Implementaion of linear search in javascript

/**
 * Linear Search ALgoritm
 * @param  wantedItem 
 * @param {Array} items 
 * @returns {(Number|Boolean)} returns index if the item is found else returns false.
 */
const LinearSearchAlgorithm = (wantedItem, items) => {
    for (let i = 0; i < items.length; i++){
        if (wantedItem == items[i]) return i;
    };
    return false;
}

Exercise

Write a program that returns True if user's child can enter primary school if not returns False
Permited Ages to enter primary school: 5,6,7,8 (Array | list).
input : child's age (integer).
example 1
input : 7
output => True
example 2
input : 3
output => False

References and useful Resources

#day_5


This content originally appeared on DEV Community and was authored by Aya Bouchiha


Print Share Comment Cite Upload Translate Updates
APA

Aya Bouchiha | Sciencx (2021-06-17T21:05:59+00:00) Linear Search Algorithm. Retrieved from https://www.scien.cx/2021/06/17/linear-search-algorithm/

MLA
" » Linear Search Algorithm." Aya Bouchiha | Sciencx - Thursday June 17, 2021, https://www.scien.cx/2021/06/17/linear-search-algorithm/
HARVARD
Aya Bouchiha | Sciencx Thursday June 17, 2021 » Linear Search Algorithm., viewed ,<https://www.scien.cx/2021/06/17/linear-search-algorithm/>
VANCOUVER
Aya Bouchiha | Sciencx - » Linear Search Algorithm. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/06/17/linear-search-algorithm/
CHICAGO
" » Linear Search Algorithm." Aya Bouchiha | Sciencx - Accessed . https://www.scien.cx/2021/06/17/linear-search-algorithm/
IEEE
" » Linear Search Algorithm." Aya Bouchiha | Sciencx [Online]. Available: https://www.scien.cx/2021/06/17/linear-search-algorithm/. [Accessed: ]
rf:citation
» Linear Search Algorithm | Aya Bouchiha | Sciencx | https://www.scien.cx/2021/06/17/linear-search-algorithm/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.