Linear search is also called as sequential search. It is a method for finding a particular value in a list, with O(n) time complexity where n is the total number of elements in the list.
Searching process of linear search take the element which we need to find and match it with first element in the list, if it matches then we have found the element and we are done else we compare the next element and keep searching until list all elements are not checked.
If we have checked all the element and searched element is found, we can show success and position(if needed) to user else not found message.
Algorithm to implment linear search
- Get the element from the user
- Compare the value from first element of the list/array.
- If both are matched, print "Element found" and end the search
- Else if both are not matched, compare it with next(second) element in the list.
- Repeat the above step 3&4 until element is found or we reach at the end of the list.
- After complete list is searched and we still have not found the element, display "Elemen not found" to user.
Simple Linear search program in C
#include<stdio.h>
#include<conio.h>
void main(){
int arr[10],size,i,Element;
//ask the user for the size of the the array
printf("Enter size of the array: ");
scanf("%d",&size);
// ask user to provide values of array
printf("Enter any %d integer values: ",size);
//save user values in arr[] array
for(i = 0; i < size; i++)
{
scanf("%d",&arr[i]);
}
printf("Enter the element to be Search: ");
scanf("%d",&Element);
// loop each element of the array and check if element is equal to the element
//we need to find
for(i = 0; i < size; i++)
{
if(Element == arr[i])
{
// show element found message with it's position
printf("Element is found at %d index", i);
break;
}
}
//check if we have reached the end of the list and not found element yet
if(i == size)
{
printf("Given element is not found in the array!!!");
}
getch();
}
Executing above program here, gives me output as below
Enter size of the array: 5
Enter any 5 integer values: 10 20 30 40 50
Enter the element to be Search: 20
Element is found at 1 index
Linear search program in C for checking multiple occurences of an element
There isn't much of the changes here, we will just not break the loop and wil keep searching the element till the end of the list, and count each element of the occurance of the element, if found increase the count value by 1, if at the end of the program count value is equal to 0, then we didn't found the element at all in the list.
#include <stdio.h>
void main()
{
int arr[10], Element, i, size, count = 0;
//ask the user for the size of the the array
printf("Enter size of the array:\n");
scanf("%d", &size);
// ask user to provide values of array
printf("Enter any %d integer values:",size);
//save the value in array
for (i = 0; i < size; i++)
{
scanf("%d", &arr[i]);
}
printf("Enter the element to be Search:\n");
scanf("%d", &Element);
for (i = 0; i < size; i++) {
if (arr[i] == Element) {
//print element position
printf("%d is present at location %d.\n", Element, i+1);
//increase count value
count++;
}
}
//if count is 0 after looping all the elements
//then element is not found
if (count == 0)
{
printf("%d isn't present in the array.\n", Element);
}
else
{
printf("%d is present %d times in the array.\n", Element, count);
}
}
Executing the above program here, gives me output as below
Enter size of the array:
5
Enter any 5 integer values:10 20 20 30 40
Enter the element to be Search:
20
20 is present at location 2.
20 is present at location 3.
20 is present 2 times in the array.
Linear search program using seperate function
#include <stdio.h>
int linear_search_function(int [], int, int);
void main()
{
int arr[10], Element, i, size, position = 0;
//ask the user for the size of the the array
printf("Enter size of the array:\n");
scanf("%d", &size);
// ask user to provide values of array
printf("Enter any %d integer values:",size);
//save the value in array
for (i = 0; i < size; i++)
{
scanf("%d", &arr[i]);
}
printf("Enter the element to search:\n");
scanf("%d", &Element);
position = linear_search_function(arr, size, Element);
//if position is -1 after looping all the elements
//then element is not found
if (position == -1)
{
printf("%d isn't present in the array.\n", Element);
}
else
{
printf("%d is present at position %d in the array.\n", Element, position);
}
}
int linear_search_function(int arr[], int size, int Element) {
int i;
for (i = 0; i < size; i++) {
if (arr[i] == Element) {
return i;
}
}
return -1;
}
Output, after executing it online
Enter size of the array:
5
Enter any 5 integer values:1020 8 9 10 20
Enter the element to search:
10
10 is present at position 3 in the array.
Linear search program in C using pointers
#include <stdio.h>
int linear_search_function(int [], int, int);
void main()
{
int arr[10], Element, i, size, position = 0;
//ask the user for the size of the the array
printf("Enter size of the array:\n");
scanf("%d", &size);
// ask user to provide values of array
printf("Enter any %d integer values:",size);
//save the value in array
for (i = 0; i < size; i++)
{
scanf("%d", &arr[i]);
}
printf("Enter the element to search:\n");
scanf("%d", &Element);
position = linear_search_function(arr, size, Element);
//if position is -1 after looping all the elements
//then element is not found
if (position == -1)
{
printf("%d isn't present in the array.\n", Element);
}
else
{
printf("%d is present at position %d in the array.\n", Element, position);
}
}
int linear_search_function(int *pointer, int size, int Element) {
int i;
for (i = 0; i < size; i++) {
if (*(pointer+i) == Element) {
return i;
}
}
return -1;
}
Output:
Enter size of the array:
50
Enter any 5 integer values:10 20 30 70 80
Enter the element to search:
70
70 is present at position 3 in the array.
You may also like :
how to set, toggle and clear a bit in C or C++ language?
Program for Matrix multiplication in C (With & Without pointers)
Creating palindrome program in C (checking string & number)
Feel free to ask any questions related to this program in comment's section.