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
- Start.
- Read the number of elements n.
- Read the array elements.
- Read the element to be searched (key).
- Initialize i = 0.
- Compare key with a[i].
- If a[i] == key, display the position and stop searching.
- Otherwise, increment i and repeat until all elements are checked.
- If the element is not found, display "Element not found".
- 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.
Best case:
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.
Worst case:
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:
Using the sum formula:
Therefore, the average case is .
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 | |
| Average case (successful) | (n+1)/2 | |
| Worst case | n |
Advantages and disadvantages
Advantages
- Simple and easy to understand.
- Easy to implement.
- Works with sorted and unsorted arrays.
- Suitable for small datasets.
- Does not require additional memory.
Disadvantages
- Slow for large datasets.
- May require checking every element.
- Less efficient than binary search for large, sorted arrays.