An Interest In:
Web News this Week
- April 24, 2024
- April 23, 2024
- April 22, 2024
- April 21, 2024
- April 20, 2024
- April 19, 2024
- April 18, 2024
June 17, 2021 09:05 pm GMT
Original Link: https://dev.to/ayabouchiha/linear-search-algorithm-31dn
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 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)
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
- https://www.bbc.co.uk/bitesize/guides/z7kkw6f/revision/7#:~:text=A%20linear%20search%20is%20the,algorithm%20must%20deal%20with%20this.
- https://www.geeksforgeeks.org/linear-search/
- https://en.wikipedia.org/wiki/Linear_search
- https://www.tutorialspoint.com/data_structures_algorithms/linear_search_algorithm.htm
- https://www.geeksforgeeks.org/linear-search/
- https://www.tutorialspoint.com/linear-search-in-python-program
- https://www.youtube.com/watch?v=iwo5WAldDks
- https://www.youtube.com/watch?v=4GPdGsB3OSc
#day_5
Original Link: https://dev.to/ayabouchiha/linear-search-algorithm-31dn
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To