Linear Search or Sequential Search in Python

Linear Search

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 L0 …. Ln−1, and target value T, the following subroutine uses linear search to find the index of the target T in L.

  1. Set i to 0.
  2. If Li = T, the search terminates successfully; return i.
  3. Increase i by 1.
  4. 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 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.

  1. Set i to 0.
  2. If Li = T, go to step 4.
  3. Increase i by 1 and go to step 2.
  4. 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 L0L1 … ≤ 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.

  1. Set i to 0.
  2. If LiT, go to step 4.
  3. Increase i by 1 and go to step 2.
  4. If Li = 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.

Instagram

Facebook

Leave a Reply

Your email address will not be published. Required fields are marked *