Linear Search Algorithm

#include<stdio.h>
#include<conio.h>
void LinearSearch(int [],int,int);
int a[100],i,n,key;
void main(){
printf("Enter array size: ");
scanf("%d",&n);
printf("Enter array elements: ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter value to be searched: ");
scanf("%d",&key);
LinearSearch(a,n,key);
getch();
}

void LinearSearch(int a[],int n,int key){
for(i=0;i<n;i++){
if(key==a[i]){
printf("%d is found at %d position.",key,i+1);
break;
}
}
if(i==n)
printf("%d is not found.",key);
}




No comments:

Post a Comment