Linear Search :
It is simplest form of searching of a item in a array. let suppose we have an “array arr “.
arr[6] = { 1, 3, 4, 5, 6, 7 };
and want to search an “element item ” in our array arr.
item = 5 ;
here we check for each element of array that where it exists.
let see how to do it through program.
#include<stdio.h> #include<conio.h> void main(){ int arr[6], i, item, pos = -1; clrscr(); printf("Enter an array"); for(i = 0; i<6; i++){ scanf("%d",&arr[i]); } printf("please enter no to search"); scanf("%d", &item); for(i=0 ; i<6 ;i++){ if(item ==arr[i]){ pos = i+1; break; } } if(pos == -1){ printf("Element not found"); } else{ printf("element found on position at : %d",pos); } getch(); }
output :