Sequential Search

Idea

Sequential Search scans from one end, comparing keys one by one until the target is found or the scan ends. Works for unordered or ordered linear lists; best for small data or low efficiency requirements.

Sequential search

Click "Step forward" to watch the algorithm state change.

When to use

  • Small linear lists
  • Unordered tables, or whenever the efficiency requirement is low

Algorithm

Pseudo-code:

for i = 0 to n-1:
    if A[i] == x:
        return i  // found
default:
    return -1     // not found

Code Implementation (with a Sentinel)

In a sequential table we can place a sentinel (the target value stored at index 00). The bounds check then disappears from every round of the loop, which speeds the search up:

// Sequential search (sentinel version); elements are stored from index 1 on.
int SeqSearch(int A[], int n, int key) {
    A[0] = key;                    // the sentinel
    int i;
    for (i = n; A[i] != key; i--);  // compare backwards, no bounds check needed
    return i;                       // i == 0 means "not found"
}

Deriving the Average Search Length (ASL)

Assume every element of the table is equally likely to be searched for, with probability 1n\frac{1}{n}.

Successful search: the comparison counts are 1,2,…,n1, 2, \ldots, n, so the average number of comparisons is

ASLsuccess=1n∑i=1ni=n+12ASL_{\text{success}} = \frac{1}{n}\sum_{i=1}^{n} i = \frac{n+1}{2}

Failed search: all nn elements are compared and then the sentinel is compared as well, giving n+1n+1 comparisons in total:

ASLfailure=n+1ASL_{\text{failure}} = n + 1

Complexity

  • Best: O(1)O(1) (first element matches)
  • Worst: O(n)O(n) (last or absent)
  • Average: O(n)O(n)

Exercises

Exercise 1

Given A=[3,7,1,9,5]A = [3, 7, 1, 9, 5], use sequential search to find 99.

Sequential SearchLinear SearchComparison

Approach: Compare one element after another until the key is hit.

Steps:

  1. Compare A[0]=3A[0]=3, which is not 99
  2. Compare A[1]=7A[1]=7, which is not 99
  3. Compare A[2]=1A[2]=1, which is not 99
  4. Compare A[3]=9A[3]=9 and the key is found

Answer: Index 33.

Exercise 2

Which structures can use sequential search, and what is its average time complexity?

Sequential SearchLinear ListTime Complexity

Approach: The scenarios and the complexity.

Steps:

  1. Applies to unordered or ordered linear lists
  2. Average time complexity O(n)O(n)

Answer: Linear lists, with an average of O(n)O(n).

Past exam

2021·408

In a sequential list of length nn, search for xx: on success swap it with its successor; on failure insert it at the end of the list. Write the algorithm and analyse the average time complexity.

Sequential SearchInsertionAverage Time Complexity

Approach: Sequential search plus swap or insert, then count the average comparisons.

Steps:

  1. Search for xx sequentially from the head of the list
  2. Once it is found, swap it with its successor
  3. If it is not found, insert it at the end of the list
  4. The average number of comparisons is (n+1)/2(n+1)/2, and a failed search needs nn comparisons
  5. Average time complexity O(n)O(n)

Answer: As shown above, O(n)O(n) on average.

Found an issue with this content?

Tell us what could be improved.