📚 C Programming Tutorial

C Programming

Learn C programming concepts with simple explanations, examples and practical notes.

← Back to C Programming

Linear Search

Introduction to linear search in C

Linear Search is a simple searching technique used to find a particular element in an array or a collection of data.

In this method, each element is compared with the search element (key), one by one, starting from the first element until:
- The required element is found, or
- All elements have been checked.

It is also called Sequential Search because elements are examined sequentially.

Example: Suppose an array contains:

3, 8, 1, 6, 9, 2, 7, 5, 4

We want to search for the element (search key): 7

Algorithm for linear search

Algorithm: Linear_Search

  1. Start.
  2. Read the number of elements n.
  3. Read the array elements.
  4. Read the element to be searched (key).
  5. Initialize i = 0.
  6. Compare key with a[i].
  7. If a[i] == key, display the position and stop searching.
  8. Otherwise, increment i and repeat until all elements are checked.
  9. If the element is not found, display "Element not found".
  10. Stop.

C program to implement linear search

//A non-recursive function to search for a Key value in a given list of integers using linear search method.
#include <stdio.h>

int main() {
    int n, i, key, arr[20],flag=-1;
    printf("Enter number of elements: ");
    scanf("%d", &n);

    printf("Enter %d integers:\n", n);
    for (i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }

    printf("Enter key to search: ");
    scanf("%d", &key);

    for (int i = 0; i < n; i++) 
    {
        if (arr[i] == key)
        {
            flag = i;   // return index if key is found
            break;
        }
            
    }
    if (flag == -1)
        printf("Key %d not found in the list.\n", key);
    else
        printf("Key %d found at position %d.\n", key, flag + 1);

    return 0;
}

Output:

Enter number of elements: 9
Enter 9 integers:
3 8 1 6 9 2 7 5 4
Enter key to search: 7
Key 7 found at position 7.

Time complexity of linear search

Time complexity describes how the number of operations increases when the input size increases.

Let n represent the total number of elements in an array. Linear search has three important cases.

A. Best-case time complexity

The best case occurs when the search element is present at the first position.

Example:
Array: 10, 20, 30, 40, 50
Search key: 10

Only one comparison is required.

T ( n ) = 1

Best case: O ( 1 )

B. Worst-case time complexity

The worst case occurs when the search element is present at the last position or is not present in the array.

Example:
Array: 10, 20, 30, 40, 50
Search key: 50

All five elements must be checked.

T ( n ) = n

Worst case: O ( n )

C. Average-case time complexity

The average case represents the expected number of comparisons when the key is equally likely to occur at any position.

For a successful search, assuming the key is equally likely to be at any of the n positions:

T ( n ) = 1 + 2 + 3 + … + n n

Using the sum formula:

T ( n ) = n ⁢ ( n + 1 ) 2 ⁢ n T ( n ) = n + 1 2

Therefore, the average case is O ( n ) .

For an unsuccessful search, all n elements must be compared, so the average number of comparisons is also n.

Time complexity summary

Case Comparisons Time complexity
Best case 1 O ( 1 )
Average case (successful) (n+1)/2 O ( n )
Worst case n O ( n )

Advantages and disadvantages

Advantages

  1. Simple and easy to understand.
  2. Easy to implement.
  3. Works with sorted and unsorted arrays.
  4. Suitable for small datasets.
  5. Does not require additional memory.

Disadvantages

  1. Slow for large datasets.
  2. May require checking every element.
  3. Less efficient than binary search for large, sorted arrays.