Linear Search in C
Here you will learn Linear Search algorithm and example code of Linear Search in C programming by using Array, functions, pointers, and recursion.
What is Linear Search in C?
Linear search in C is a search algorithm that sequentially checks each element of an array or list until a matching element is found or the end of the list is reached. Linear search is also known as sequentially search or naive search. It is a simple but inefficient search algorithm, as it requires a number of steps proportional to the size of the list.
Scope of Linear Search in C
Linear search is a way to search an specified item from an items list. It continuously checks each element from the list until a searched item is found, or end the list.
The scope of linear search in C is to search a list of elements for a specified item and return the index position of the item, or -1 if the item is not found.
Flowchart of Linear Search
Different Ways to Program Linear Search
Linear Search using Array in C
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 | #include<stdio.h> int main() { int arr[20], n, key, i; printf("\nEnter the number of elements: "); scanf("%d", &n); printf("\nEnter %d elements: n", n); for(i=0; i<n; i++) { scanf("%d", &arr[i]); } printf("\nEnter the element to search: "); scanf("%d", &key); for(i=0; i<n; i++) { if(arr[i] == key) { printf("\n%d found at position %d\n", key, i+1); break; } } if(i == n) { printf("\n%d not found in array\n", key); } return 0; } |
Output
Linear Search using Function in C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | #include <stdio.h> int linear_search(int arr[], int n, int x) { int i; for (i=0; i<n; i++) { if (arr[i] == x) return i; } return -1; } int main(void) { int arr[] = {2, 3, 4, 10, 40}; int x = 10; int n = sizeof(arr)/sizeof(arr[0]); int result = linear_search(arr, n, x); (result == -1)? printf("Element is not present in array") : printf("Element is present at %d position",result+1); return 0; } |
Output
Linear Search using Pointers in C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | #include<stdio.h> int linearSearch(int *arr, int n, int find) { for (int i = 0; i < n; i++) { if (arr[i] == find) return i; } return -1; } int main() { int arr[] = { 2, 4, 6, 8, 10 }; int find; int n = sizeof(arr) / sizeof(arr[0]); printf("Enter the element to find: "); scanf("%d", &find); int index = linearSearch(arr, n, find); if (index == -1) printf("Element is not present in the array"); else printf("Element is present at %d position", index+1); return 0; } |
Output
Linear Search using Recursion in C
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 | //Linear Search using Recursion in C #include <stdio.h> // Recursive linear search function int recursiveLinearSearch(int arr[], int key, int index, int size) { if (index >= size) { return -1; } // Check if the current element is equal to the key if (arr[index] == key) { return index; // return index location } else { // Recursive call to search return recursiveLinearSearch(arr, key, index + 1, size); } } int main() { int arr[] = {12, 24, 35, 57, 72, 102, 256}; int key = 57; int size = sizeof(arr) / sizeof(arr[0]); int index = recursiveLinearSearch(arr, key, 0, size); if (index != -1) { printf("Element %d found at index %d\n", key, index); } else { printf("Element %d not found in the array\n", key); } return 0; } |
Output
Element 57 found at index 3
Read Also
I like your way of explanation. Thanks for helping me.