Looking for a particular answer sheet in an unsorted bundle leaves only one option: start at the top and examine each sheet until the right one turns up. This is the sequential search, the simplest way of finding a value in an array.

The Searching Problem

Definition: Searching. The process of finding whether a given value, called the key or target, is present in a collection, and if so, at which position.

A search has two possible outcomes. It is successful when the key is found, and unsuccessful when the whole collection has been examined without finding it.

The Sequential Search Method

Definition: Sequential search. A search method that compares the key with each element of an array in turn, from the first element onwards, until a match is found or the array ends.

It is also called linear search. The array need not be in any particular order.

Reporting the Result

The variable pos starts at −1. Since −1 can never be a valid index, it serves as a signal meaning "not found". If the key is found at index i, pos is set to i and the loop is left with break. After the loop, the value of pos shows which outcome occurred.

The search compares 9 with 6, then with 2, then with 9, and succeeds at index 2 after three comparisons. A search for 4 would compare all five elements and end unsuccessfully.

Efficiency

The number of comparisons depends on where the key lies. Table 4.6 gives the three cases for an array of n elements.

Table 4.6: Comparisons made by sequential search in an array of n elements

Case Situation Comparisons
Best The key is the first element 1
Average The key is somewhere in the middle About n / 2
Worst The key is last, or absent n

The work grows in proportion to n. For 10 elements this is trivial; for a million elements an unsuccessful search needs a million comparisons.

Merits and Limits

Sequential search is simple, works on unsorted data and suits small arrays. Its weakness is slowness on large arrays. When the data is sorted, the binary search of the next topic is far faster.

Duplicate Values

If the key occurs more than once, the method as described reports the first occurrence. To find every occurrence, the break is removed and each matching index is displayed.

Example 4.6: Sequential search in an array

Problem: Search the array 6, 2, 9, 5, 7 for a key entered by the user and report its index, or that it is absent.

Solution:

  1. Start pos at −1.
  2. Compare the key with each element; on a match, record the index and leave the loop.
  3. After the loop, examine pos.
#include <stdio.h>

int main(void)
{
    int a[5] = {6, 2, 9, 5, 7};
    int n = 5, key, i, pos = -1;

    printf("Enter the value to search: ");
    scanf("%d", &key);
    for (i = 0; i < n; i++)
    {
        if (a[i] == key)
        {
            pos = i;
            break;
        }
    }
    if (pos == -1)
    {
        printf("\n%d is not in the array\n", key);
    }
    else
    {
        printf("\n%d found at index %d\n", key, pos);
    }
    return 0;
}

Input:

9

Output:

Enter the value to search:
9 found at index 2

Result: The key 9 is found at index 2 after three comparisons, and break prevents the remaining two elements from being examined.

Common mistakes

  • Continuing to search after the key has been found, wasting comparisons.
  • Reporting "not found" inside the loop on the first mismatch instead of after the loop.
  • Letting the loop run past the last element when the key is absent.

Key takeaways

  • Sequential search compares the key with each element in turn.
  • It works on unsorted arrays.
  • A position of −1 is used to signal that the key was not found.
  • The worst case needs n comparisons.

Practice questions

  1. How many comparisons does sequential search make to find 5 in the array 6, 2, 9, 5, 7?
  2. Does the array need to be sorted for sequential search?
  3. Why is −1 used as the starting value of pos?

Answers to the practice questions

  1. Four comparisons.
  2. No.
  3. It is not a valid index, so it can signal that the key was not found.

Sources

  • Official BCA syllabus (NEP 2025 scheme), Problem Solving Techniques, course code 25BCA401SEC01, Unit IV