0% found this document useful (0 votes)
2 views6 pages

Algorithms 1,2,3

The document contains three separate implementations of search algorithms in C: linear search, recursive binary search, and pattern search. Each program includes code, input/output examples, and measures the time taken to perform the search. The linear search and binary search both find specific values in an array, while the pattern search identifies occurrences of a substring within a string.

Uploaded by

raguram9024
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)
2 views6 pages

Algorithms 1,2,3

The document contains three separate implementations of search algorithms in C: linear search, recursive binary search, and pattern search. Each program includes code, input/output examples, and measures the time taken to perform the search. The linear search and binary search both find specific values in an array, while the pattern search identifies occurrences of a substring within a string.

Uploaded by

raguram9024
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/ 6

EX.

NO:1:IMPLEMENTATION OF LINEAR SEARCH WITH TIME COMPLEXITY-9024-RAMU

PROGRAM:

#include<stdio.h>

#include<time.h>

int main()

int a[10],n,s,i;int f=0;

clock_t b,e;double t;

printf("Enter n Value: ");

scanf("%d",&n);

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

printf("Enter Array Elements: ");

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

printf("Enter The Search Value: ");

scanf("%d",&s);

b=clock();

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

if(a[i]==s){

f=1;

break;}}

e=clock();

t=((double)e-b)/CLOCKS_PER_SEC;

if(f==1){

printf("\nELEMENT FOUND AT POSITION %d",i+1);}

else{

printf("\nNot found");}

printf("\nTIME TAKEN FOR LINEAR SEARCH %f",t);

return 0;

}
OUTPUT:

Enter n Value: 5

Enter Array Elements: 38

Enter Array Elements: 46

Enter Array Elements: 51

Enter Array Elements: 72

Enter Array Elements: 87

Enter The Search Value: 72

ELEMENT FOUND AT POSITION 4

TIME TAKEN FOR LINEAR SEARCH 0.000002


EX.NO:2:IMPLEMENTATION OF RECURSIVE BINARY SEARCH WITH TIME COMPLEXITY-9024-RAMU

PROGRAM:

#include<stdio.h>

#include<time.h>

int bs(int mk[],int l,int r,int s)

int mid=l+(r-l)/2;

if(r>=l){

if(mk[mid]==s){

return mid;}

if(mk[mid]>s){

return bs(mk,l,mid-1,s);}

return bs(mk,mid+1,r,s);}

return -1;

int main()

int n,mk[100],i,h,ans;

clock_t b,e;

double d;

printf("\nENTER THE VALUE: ");

scanf("%d",&n);

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

printf("\nENTER ARRAY VALUE: ");

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

printf("\nENTER VALUE TO SEARCH: ");

scanf("%d",&h);

b=clock();

ans=bs(mk,0,n-1,h);
e=clock();

if(ans==-1){

printf("\nVALUE NOT FOUND");}

else{

printf("\nVALUE FOUND AT POSITION AT %d ",ans+1);}

d=((double)e-b)/CLOCKS_PER_SEC;

printf("\nTIME TAKEN FOR BINARY SEARCH %f",d);

return 0;

OUTPUT:

ENTER THE VALUE: 5

ENTER ARRAY VALUE: 39

ENTER ARRAY VALUE: 43

ENTER ARRAY VALUE: 51

ENTER ARRAY VALUE: 68

ENTER ARRAY VALUE: 82

ENTER VALUE TO SEARCH: 51

VALUE FOUND AT POSITION AT 3

TIME TAKEN FOR RECURSIVE BINARY SEARCH 0.000002

EX.NO:3:IMPLEMENTATION OF PATTERN SEARCH-9024-RAMU


PROGRAM:

#include<stdio.h>

#include<string.h>

#include<time.h>

void s(char* t,char* p){

int m=strlen(p);

int n=strlen(t);

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

int j;

for(j=0;j<m;j++)

if(t[i+j]!=p[j]){

break;}

if(j==m){

printf("\nPATTERN FOUND AT INDEX %d",i+1);}

}}

int main(){

char t[100],p[100];

clock_t b,e;

double q;

printf("\nENTER THE TEXT: ");

fgets(t,sizeof(t),stdin);

t[strcspn(t,"\n")]='\0';

printf("\ENTER THE PATTERN: ");

fgets(p,sizeof(p),stdin);

p[strcspn(p,"\n")]='\0';

b=clock();

s(t,p);

e=clock();

q=((double)e-b)/CLOCKS_PER_SEC;
printf("\nTIME TAKEN FOR PATTERN SEARCH=%f",q);

return 0;

OUTPUT:

ENTER THE TEXT: aabcdaaaabcdabdabcd

TER THE PATTERN: abcd

PATTERN FOUND AT INDEX 2

PATTERN FOUND AT INDEX 9

PATTERN FOUND AT INDEX 16

TIME TAKEN FOR PATTERN SEARCH=0.000019

You might also like