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.

#### Basic algorithm

Given a list *L* of *n* elements with values or records *L*_{0} …. *L*_{n−1}, and target value *T*, the following subroutine uses linear search to find the index of the target *T* in *L*.

- Set
*i*to 0. - If
*L*_{i}=*T*, the search terminates successfully; return*i*. - Increase
*i*by 1. - If
*i*<*n*, go to step 2. Otherwise, the search terminates unsuccessfully.

#### With a sentinel

The basic algorithm above makes two comparisons per iteration: one to check if *L*_{i} equals *T*, and the other to check if *i* still points to a valid index of the list.

By adding an extra record *L*_{n} 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.

- Set
*i*to 0. - If
*L*_{i}=*T*, go to step 4. - Increase
*i*by 1 and go to step 2. - If
*i*<*n*, the search terminates successfully; return*i*. Else, the search terminates unsuccessfully.

#### In an ordered table

If the list is ordered such that *L*_{0} ≤ *L*_{1} … ≤ *L*_{n−1}, the search can establish the absence of the target more quickly by concluding the search once *L*_{i} exceeds the target. This variation requires a sentinel that is greater than the target.

- Set
*i*to 0. - If
*L*_{i}≥*T*, go to step 4. - Increase
*i*by 1 and go to step 2. - If
*L*_{i}=*T*, the search terminates successfully; return*i*. Else, the search terminates unsuccessfully.

#### Sequential Search

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

#### Ordered List

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

#### Follow Me

If you like my post please follow me to read my latest post on programming and technology.