C Program For Linear Search Algorithm
Learn How To Find an Element in 1-Dimensional Array using Linear Search in C Programming Language. It is important that we should know How A For Loop Works before getting further with the C Program Code. Linear Searching is also popularly known as Sequential Search Technique.
Linear Search Algorithm
To Find an Element in an Array using Sequential Search Algorithm Technique, we need to traverse over the complete Array and Compare every Element of the Array with the Search Element. If the Element matches with Search Element, we break out of the Loop in C Program.
Sequential Search Algorithm Analysis
The worst case scenario of this algorithm is when the element is not found in the Array. The best case of the algorithm is when the element is found at the first position in the Array. The Runtime Complexity of Sequential Search Algorithm in Worst Case Scenario as well as Best Case Scenario is 0(n). Linear Search Method is good to implement in Data Structures such as Linked Lists.
Must Read: C Program To Sort Arrays using Binary Search
C Program For Linear Search Algorithm using Functions
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | #include<stdio.h> int Linear_Search(int arr[], int limit, int element) { int count = 0; while(count < limit && element != arr[count]) { count++; } if(count < limit) { return count; } else { return -1; } } int main() { int arr[10], count, element, position; printf("\nEnter 10 Elements in Array:\t"); for(count = 0; count < 10; count++) { scanf("%d", &arr[count]); } printf("\nElements in Array are:\n"); for(count = 0; count < 10; count++) { printf("%d\t", arr[count]); } printf("\n"); printf("\nEnter Element To Search:\t"); scanf("%d", &element); position = Linear_Search(arr, 10, element); if(position == -1) { printf("\nElement %d Not Found in Array\n", element); } else { printf("\nElement %d found at Position %d\n", element, position + 1); } return 0; } |
Must Read: C Program To Implement Binary Search Algorithm using Recursion
C Program To Search Element in Array using Linear Search without Function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | #include<stdio.h> int main() { int arr[10], count, element, search = 0; printf("\nEnter 10 Elements in Array:\t"); for(count = 0; count < 10; count++) { scanf("%d", &arr[count]); } printf("\nElements in Array are:\n"); for(count = 0; count < 10; count++) { printf("%d\t", arr[count]); } printf("\n"); printf("\nEnter Element To Search:\t"); scanf("%d", &element); for(count = 0; count < 10; count++) { if(arr[count] == element) { printf("\nElement %d found at Position %d\n", element, count + 1); search++; } } if(search == 0) { printf("\nElement %d Not Found in Array\n", element); } printf("\n"); return 0; } |
Output

In case you get any Compilation Errors with this C Program Code To Search Array Element with Linear Search Algorithm or if you have any doubts about it, let us know about it in the Comment Section below.
Best Explanation to Search Array Element using Linear Search Algorithm in C Programming. Thanks a lot!
please write the algorithm of linear searching