In computer science, a linear search or sequential search is a method for finding an element within a list. It sequentially checks each element of the list until a match is found or the whole list has been searched.
A linear search sequentially checks each element of the list until it finds an element that matches the target value. If the algorithm reaches the end of the list, the search terminates unsuccessfully.
A linear search runs in at worst linear time and makes at most n comparisons, where n is the length of the list. If each element is equally likely to be searched, then linear search has an average case of n+1/2 comparisons, but the average case can be affected if the search probabilities for each element vary.
Linear search is rarely practical because other search algorithms and schemes, such as the binary search algorithm and hash tables, allow significantly faster searching for all but short lists.
Given a list L of n elements with values or records L0 …. Ln−1, and target value T, the following subroutine uses linear search to find the index of the target T in L.
The basic algorithm above makes two comparisons per iteration: one to check if Li equals T, and the other to check if i still points to a valid index of the list.
By adding an extra record Ln to the list (a sentinel value) that equals the target, the second comparison can be eliminated until the end of the search, making the algorithm faster. The search will reach the sentinel if the target is not contained within the list.
If the list is ordered such that L0 ≤ L1 … ≤ Ln−1, the search can establish the absence of the target more quickly by concluding the search once Li exceeds the target. This variation requires a sentinel that is greater than the target.
def seq_search(arr,ele): """ General Sequential Search. Works on Unordered lists. """ # Start at position 0 pos = 0 # Target becomes true if ele is in the list found = False # go until end of list while pos < len(arr) and not found: # If match if arr[pos] == ele: found = True # Else move one down else: pos = pos+1 return found
arr = [1,9,2,8,3,4,7,5,6]
print(seq_search(arr,1))
True
print(seq_search(arr,10))
False
If we know the list is ordered than, we only have to check until we have found the element or an element greater than it.
def ordered_seq_search(arr,ele): """ Sequential search for an Ordered list """ # Start at position 0 pos = 0 # Target becomes true if ele is in the list found = False # Stop marker stopped = False # go until end of list while pos < len(arr) and not found and not stopped: # If match if arr[pos] == ele: found = True else: # Check if element is greater if arr[pos] > ele: stopped = True # Otherwise move on else: pos = pos+1 return found
arr.sort()
ordered_seq_search(arr,3)
True
ordered_seq_search(arr,1.5)
False
If you like my post please follow me to read my latest post on programming and technology.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…
View Comments
When I originally left a comment I appear to have clicked the -Notify me when new comments are added- checkbox and from now on every time a comment is added I recieve four emails with the same comment. There has to be a way you can remove me from that service? Appreciate it!| Ethelin Hamid Holzman