Introduction
Linear search is a searching algorithm used to find a specific element in an array. Linear search checks each element sequentially until the target value is found or every element has been examined. A linear search program in C helps beginners understand arrays, loops, conditional statements, and basic algorithm implementation. Linear search works with both unsorted arrays, making it easy to apply in different programming situations. Learning how linear search works, writing the code, and analyzing its complexity builds a strong foundation in C programming.
What Is Linear Search in C?
Linear search is a searching algorithm that compares a target value with each array element one by one.
For example, consider this array:
int arr[] = {10, 20, 30, 40, 50};
If the target value is 30, linear search compares the elements in this order:
- Compare 30 with 10: No match.
- Compare 30 with 20: No match.
- Compare 30 with 30: Match found.
The search ends once the value is found. If the value is not found, the search checks all items in the list before saying the value was not found.
Algorithm for Linear Search
The algorithm follows a straightforward sequence of steps:
- Start the program.
- Read the number of array elements.
- Accept the array elements from the user.
- Read the target value.
- Compare the target with each array element.
- If a match is found, display its position and stop searching.
- If no match is found, display an appropriate message.
- End the program.
This approach makes a C program for linear search easy to write, understand, and test.
Linear Search Program in C
The following C program searches for a given integer in an array and displays its position.
#include <stdio.h>
int main(void) {
int arr[100], n, key, i;
int found = 0;
printf(“Enter the number of elements: “);
scanf(“%d”, &n);
if (n < 1 || n > 100) {
printf(“Invalid array size.\n”);
return 1;
}
printf(“Enter the array elements:\n”);
for (i = 0; i < n; i++) {
scanf(“%d”, &arr[i]);
}
printf(“Enter the element to search: “);
scanf(“%d”, &key);
for (i = 0; i < n; i++) {
if (arr[i] == key) {
printf(“Element found at position %d.\n”, i + 1);
found = 1;
break;
}
}
if (!found) {
printf(“Element not found.\n”);
}
return 0;
}
Example Output
Enter the number of elements: 5
Enter the array elements:
10 20 30 40 50
Enter the element to search: 30
Element found at position 3.
The program finds 30 at the third position and stops searching.
If the target is 60, the output is:
Element not found.
Learn programming fundamentals with easy-to-understand C Programming Tutorials for Beginners.
How Does the C Program for Linear Search Work?
The program uses a few basic C programming concepts to perform the search.
- Array declaration: int arr[100] builds an array that can hold up to 100 integers.
- User input: scanf() gathers the array size, the elements, and the target value.
- For loop: Iterates through the array from index 0 to n – 1.
- If condition: arr[i] == key checks if the current element matches the target value.
- Found variable: The variable found indicates whether the element has been located.
- Break statement: halts the loop after the first match is found.
- Position display: i + 1 shows the position starting from one, even though C array indexing starts at zero.
The program also validates the array size and input values to prevent input from being used in the search.
Time and Space Complexity of Linear Search
Linear search checks elements one by one. Linear search performance depends on the target’s location. Linear search is easy to understand.
Time Complexity
- Best case — O(1): The target is the first element, requiring one comparison.
- Average case — O(n): The algorithm checks multiple elements before finding the target.
- Worst case — O(n): The target is the last element or is absent, requiring a complete search.
Here, n represents the number of elements in the array.
Space Complexity
Linear search takes O(1) space because it uses only a fixed number of extra variables during the search. The array itself needs storage that depends on the number of items.
Advantages and Limitations of Linear Search
Linear search is useful for searching tasks, especially when the dataset is small.
Advantages:
- Easy to implement and understand.
- Works with unsorted arrays.
- Does not require sorting before searching.
- Uses little additional memory.
Limitations:
- May examine every element when the target is absent.
- Can be slow for large datasets.
- Repeated searches may require comparisons.
For large, sorted arrays, binary search can be a better option because it reduces the search range by half in each step. However, binary search requires sorted data.
Conclusion
Linear search provides a straightforward way to find elements in an array using C programming. Understanding its implementation, algorithm, and time complexity helps build essential problem-solving skills. Practising searching algorithms strengthens programming fundamentals. Prepares learners for more advanced concepts. For programming guidance and career support, a Training and Placement Institute in Chennai can help develop industry‑relevant skills.
