Linear Search is a simple searching algorithm used to find a particular element in an array or list. It works by checking each element one by one, starting from the first element, until the required element is found or the entire collection has been searched.
Linear Search is also known as Sequential Search because it examines elements sequentially.
In this searching technique, each element is compared with the target value (also called the key). If a match is found, the algorithm returns the position or index of the element. If the target element is not present, the algorithm returns -1 or displays an appropriate message.
Important points about Linear Search:
- It works with both sorted and unsorted arrays.
- It checks elements sequentially, one at a time.
- It does not require additional memory for searching.
- Its best-case time complexity is O(1).
- Its average and worst-case time complexity is O(n).
- It is suitable for small datasets and simple searching operations.
Example: Suppose an array contains [10, 25, 30, 45, 50] and we want to search for 30. Linear Search compares 30 with 10, then 25, and finally 30. Since the target is found at index 2, the search stops and returns index 2 (third position).

Working of Linear Search
Linear Search follows a straightforward process. It starts checking the elements from the beginning of the array and continues until it finds the required value.
Consider the following array:
A = [70, 40, 30, 50, 25, 60]
Target element (Key) = 25
The searching process takes place as follows:
| Step | Current Element | Comparison | Result |
|---|---|---|---|
| 1 | 70 | 70 == 25 | Not matched |
| 2 | 40 | 40 == 25 | Not matched |
| 3 | 30 | 30 == 25 | Not matched |
| 4 | 50 | 50 == 25 | Not matched |
| 5 | 25 | 25 == 25 | Matched |
Final Result: Element 25 is found at index 4 (5th position).
Note: If the target element is not found after checking all the elements, the algorithm terminates and returns -1 to indicate an unsuccessful search.
Algorithm: Linear Search
- Start.
- Read the number of elements n.
- Read the array elements.
- Read the target element X.
- Initialize the loop variable i = 0.
- Compare A[i] with X.
- If A[i] == X, return i and stop searching.
- Otherwise, increment i by 1.
- Repeat steps 6–8 until i reaches n.
- If the element is not found, return -1.
- Stop.
Pseudocode of Linear Search
The pseudocode represents the logical steps of Linear Search without depending on any specific programming language.
LINEAR_SEARCH(A, n, X)
1. SET i = 0
2. WHILE i < n
IF A[i] == X
RETURN i
END IF
i = i + 1
END WHILE
3. RETURN -1
4. STOP
Implement Linear Search in C
#include <stdio.h>
int main() {
int arr[100], n, key, i;
int found = 0;
printf("Enter the number of elements: ");
scanf("%d", &n);
printf("Enter %d elements:\n", n);
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Enter the element to search: ");
scanf("%d", &key);
// Perform Linear Search
for (i = 0; i < n; i++) {
if (arr[i] == key) {
printf("Element found at position %d\n", i + 1);
found = 1;
break;
}
}
if (found == 0) {
printf("Element not found in the array.\n");
}
return 0;
}
Output 1: When the Element Is Found
Enter the number of elements: 5
Enter 5 elements:
10
20
30
40
50
Enter the element to search: 30
Element found at position 3
Output 2: When the Element Is Not Found
Enter the number of elements: 5
Enter 5 elements:
10
20
30
40
50
Enter the element to search: 25
Element not found in the array.
Time Complexity of Linear Search
| Case | Time Complexity | Explanation |
|---|---|---|
| Best Case | O(1) | The target element is found at the first position. |
| Average Case | O(n) | The algorithm checks approximately half of the elements when searching for an existing element in a randomly ordered array. |
| Worst Case | O(n) | The target is at the last position or is not present in the array. |
1. Best-Case Time Complexity – O(1)
The best case occurs when the target element is present at the first index of the array.
Example:
A = [10, 20, 30, 40, 50]
Target = 10
The algorithm compares the first element with the target and immediately finds a match.
Only one comparison is required, regardless of the total number of elements.
Therefore, the best-case time complexity is O(1).
2. Average-Case Time Complexity – O(n)
The average case occurs when the target element is located at a random position in the array.
For a successful search, assuming each position is equally likely, the average number of comparisons is:
Where n represents the total number of elements.
For example, if an array contains 100 elements, the expected number of comparisons is 50.5.
Since the number of comparisons increases proportionally with the input size, the average-case time complexity is O(n).
3. Worst-Case Time Complexity – O(n)
The worst case occurs in either of these situations:
- The target element is located at the last position.
- The target element does not exist in the array.
Example:
A = [10, 20, 30, 40, 50]
Target = 60
The algorithm compares the target with every element in the array but does not find a match.
Therefore, it performs n comparisons.
The worst-case time complexity is O(n).
Applications of Linear Search
Linear Search is useful in situations where the dataset is small, unsorted, or stored in a sequential structure.
Some common applications include:
- Searching Student Records: Finding a student’s roll number in a small list.
- Finding Products: Checking whether a particular product exists in a simple product list.
- Searching Linked Lists: Finding a node containing a specific value.
- Finding an Element in an Unsorted Array: Searching without sorting the elements first.
- Checking User Input: Verifying whether a particular value exists in a small collection.
- Finding the First Occurrence: Locating the first occurrence of a particular element in an array.