Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
42 views3 pages

Linear Search

The first code snippet is a C program that searches for a specified element in an array and counts the number of steps taken to find it. The second snippet enhances the first by measuring the CPU time taken for the search operation. Both programs prompt the user for the number of elements and the elements of the array, as well as the element to search for.

Uploaded by

Kukum Chouhan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views3 pages

Linear Search

The first code snippet is a C program that searches for a specified element in an array and counts the number of steps taken to find it. The second snippet enhances the first by measuring the CPU time taken for the search operation. Both programs prompt the user for the number of elements and the elements of the array, as well as the element to search for.

Uploaded by

Kukum Chouhan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

1.

#include <stdio.h>

int main()

int n,a[10],sno,flag=0,count=0;

printf("Enter the no of elements of array ");

scanf("%d",&n);

printf("enter the elements of array ");

for(int i=0;i<n;i++)

scanf("%d",&a[i]);

printf("enter the element to search ");

scanf("%d",&sno);

for(int i=0;i<n;i++){

count+=1;

if(a[i]==sno){

flag=1;

break;

if(flag==1){

printf("%d is found\n",sno);

printf("%d is no of steps",count);}

else{

printf("%d is not found\n",sno);

printf("%d is no of steps",count);

}
return 0;

2.

#include <stdio.h>

#include<time.h>

int main()

clock_t start,end;

int n,a[10],sno,flag=0,count=0;

double total=0;

start=clock();

printf("Enter the no of elements of array ");

scanf("%d",&n);

printf("enter the elements of array ");

for(int i=0;i<n;i++)

scanf("%d",&a[i]);

printf("enter the element to search ");

scanf("%d",&sno);

for(int i=0;i<n;i++){

count+=1;
if(a[i]==sno){

flag=1;

break;

end=clock();

total=(double)(end-start)/CLOCKS_PER_SEC;

if(flag==1){

printf("%d is found\n",sno);

printf("%d is no of steps\n",count);}

else{

printf("%d is not found\n",sno);

printf("%d is no of steps\n",count);

printf("total time taken by the cpu:%f",total);

return 0;

You might also like