Вы находитесь на странице: 1из 27

1..

Searching
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<time.h>
int binary_search(int key, int a[], int low, int high);
int linear_search(int a[], int key, int n);
void main()
{
int a[1000],n,key,i,pos,ch;
clock_t st, end, total_t;
clrscr();
while(1)
{
printf("\n1.Linear search\n2.Binary search\n3.Exit\nEnter the choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:st=clock();
printf("Enter the number of elements: ");
scanf("%d",&n);
printf("\n Enter the elements order: \n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\n Enter the element to be searched: ");
scanf("%d", &key);
pos=linear_search(a,key,n);
end=clock();
if(pos==-1)
printf("\n Element not found");
else
printf("\n Element found at position %d",pos+1);
total_t=(end-st)/CLK_TCK;
printf("\n Time taken to find the element in the array is: %ld", total_t);
break;
case 2:st=clock();
printf("Enter the number of elements: ");
scanf("%d",&n);
printf("\n Enter the elements in ascending order: \n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}

printf("\n Enter the element to be searched: ");


scanf("%d", &key);
pos=binary_search(key,a,0,n-1);
end=clock();
if(pos==-1)
printf("\n Element not found");
else
printf("\n Element found at position %d",pos+1);
total_t=(end-st)/CLK_TCK;
printf("\n Time taken to find the element in the array is: %ld", total_t);
break;
case 3: exit(0);
}
}
getch();
}
int linear_search(int a[], int key, int n)
{
if(n<0)
return -1;
if(key==a[n])
return n;
return linear_search(a,key,n-1);
}
int binary_search(int key, int a[], int low, int high)
{
int mid;
if(low>high)
return -1;
mid=(low+high)/2;
if(key==a[mid])
return mid;
else if (key<a[mid])
return binary_search(key,a,low,mid-1);
else
return binary_search(key,a,mid+1,high);
}

2.SELECTION SORT
#include<time.h>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int n, a[20];
void read_data();
void display_data();
void sort_data();
void read_data()
{
int i;
printf("Enter the number of elements: ");
scanf("%d", &n);
printf("\n Enter the array elements: \n ");
for(i=0; i<n; i++)
{
scanf("%d", &a[i]);
}
}
void sort_data()
{
int pos, i, j,temp;
for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if (a[j] < a[pos])
pos=j;
}
temp= a[pos];
a[pos]=a[i];
a[i]=temp;
}
}
void display_data()
{

int i;
printf("\n The sorted array is: ");
for(i=0;i<n;i++)
printf("\n %d",a[i]);
}
void main()
{
clock_t st, end, total_t;
st=clock();
clrscr();
read_data();
sort_data();
display_data();
end=clock();
total_t=(end-st)/CLK_TCK;
printf("\n The time taken to sort is %ld",total_t);
getch();
}

3.QUICK SORT
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<time.h>
int a[15],n,i;
void read_data();
void display_data();
void quick_sort(int,int);
int partition(int,int);
void read_data()
{
printf("Enter limit:\t");
scanf("%d",&n);
printf("Enter elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
}
void display_data()
{
printf("\n\nSorted list:\n");
for(i=0;i<n;i++)
printf("\t%d",a[i]);
}
void quick_sort(int low,int high)
{
int mid;
if(low<high)
{
mid=partition(low,high);
quick_sort(low,mid);
quick_sort(mid+1,high);
}
}
int partition(int low,int high)
{
int i,j,temp,key;
key=a[low];
i=low+1;
j=high;
while(1)
{
while(i<high && key>=a[i])
i++;
while(key<a[j])
j--;
if(i<j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
else

{
temp=a[low];
a[low]=a[j];
a[j]=temp;
return j;
}
}
}
void main()
{
clock_t end,st,total_t;
clrscr();
st=clock();
read_data();
quick_sort(0,n-1);
end=clock();
display_data();
total_t=(end-st)/CLK_TCK;
printf("\n\nTime taken= %ld",total_t);
getch();
}

4.MERGE SORT
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<time.h>
int a[15],c[15],i,n;
void read_data();
void display_data();
void mergesort();
void read_data()
{
printf("Enter limit:\t");
scanf("%d",&n);
printf("Enter elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
}
void display_data()
{
printf("\n\nSorted list:\n");
for(i=0;i<n;i++)
printf("\t%d",a[i]);
}
void merge(int low,int mid,int high)
{
int i,j,k;
i=low;
j=mid+1;
k=low;
while(i<=mid && j<=high)
{
if(a[i]<a[j])
{
c[k]=a[i];
k++;
i++ ;
}
else
{
c[k]=a[j];
k++;
j++;
}
}
while(i<=mid)
{
c[k]=a[i];
k++;
i++;
}
while(j<=high)
{
c[k]=a[j];

k++;
j++;
}
for(i=0;i<=k-1;i++)
a[i]=c[i];
}
void mergesort(int low,int high)
{
int mid;
if(low<high)
{
mid=(low+high)/2;
mergesort(low,mid);
mergesort(mid+1,high);
merge(low,mid,high);
}
}
void main()
{
clock_t end,st,total_t;
clrscr();
st=clock();
read_data();
mergesort(0,n-1);
end=clock();
display_data();
total_t=(end-st)/CLK_TCK;
printf("\n\nTime taken= %ld",total_t);
getch();
}

5.INSERTION SORT
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<time.h>
int a[15],i,n;
void read_data();
void display_data();
void insertion_sort();
void read_data()
{
printf("Enter limit:\t");
scanf("%d",&n);
printf("Enter elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
}
void display_data()
{
printf("\n\nSorted list:\n");
for(i=0;i<n;i++)
printf("\t%d",a[i]);
}
void insertion_sort()
{
int j,item;
for(i=0;i<n;i++)
{
item=a[i];
j=i-1;
while(j>=0 && item<=a[j])
{
a[j+1]=a[j];
j--;
}
a[j+1]=item;
}
}
void main()
{
clock_t end,st,total_t;
clrscr();
st=clock();
read_data();
insertion_sort();
end=clock();
display_data();
total_t=(end-st)/CLK_TCK;
printf("\n\nTime taken= %ld",total_t);
getch();
}

6. TOWER OF HANOI
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<conio.h>
#include<time.h>
void Hanoi(int, char, char, char);
void Hanoi(int n, char S, char I, char D)
{
if(n==1)
{
printf("\n Move disk 1 from peg %c to %c ", S,D);
return;
}
Hanoi(n-1,S,D,I);
printf("\n Move disk %d from peg %c to peg %c", n,S,D);
Hanoi(n-1,I,S,D);
}
void main()
{
int n, count;
clock_t st, end, total_t;
clrscr();
st=clock();
printf("\n Enter the number of disks : \t");
scanf("%d",&n);
printf("\n Tower of Hanoi involves the moves : \n");
Hanoi(n,'S','I','D');
end=clock();
total_t= (end-st)/CLK_TCK;
count= pow(2,n)-1;
printf("\n The number of moves required is %d", count);
printf("\n Total time taken is %ld", total_t);
getch();
}

7. FIBONACCI
#include<stdio.h>
#include<conio.h>
#include<time.h>
#include<stdlib.h>
int Fibonacci(int);
void main()
{
int n, i, c=0;
clock_t st, end, total_t;
clrscr();
st=clock();
printf("\n Enter the number of elements: ");
scanf("%d",&n);
printf("\n \n Fibonacci series \n");
for ( i = 1 ; i <= n ; i++ )
{
printf("%d\n", Fibonacci(c));
c++;
}
end=clock();
total_t=(end-st)/CLK_TCK;
printf("\n \n \n \n Time taken to find the element the series is: %ld", total_t);
getch();
}
int Fibonacci(int n)
{
if ( n == 0 )
return 0;
else if ( n == 1 )
return 1;
else
return ( Fibonacci(n-1) + Fibonacci(n-2) );
}

8..DFS
#include<stdio.h>
#include<conio.h>
int a[20][20],reach[20],n;
void dfs(int v)
{
int i;
reach[v]=1;
for(i=1;i<=n;i++)
if(a[v][i] && ! reach[i])
{
printf("\n%d->%d",v,i);
dfs(i);
}
}
void main()
{
int i,j,count=0;
clrscr();
printf("\n enter number of vertices");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
reach[i]=0;
for(j=1;j<=n;j++)
a[i][j]=0;
}
printf("enter the adjacency matrix");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
dfs(1);
printf("\n");
for(i=1;i<=n;i++)
{
if(reach[i])
count++;
}
if(count==n)
printf("\n graph is connected");
else
printf("\n graph is not connected");
getch();
}

9. TOPOLOGICAL SORT
#include<stdio.h>
#include<conio.h>
void read_data();
void topological_sort();
void find_degree();
int n, a[10][10], indegree[10];
void main()
{
clrscr();
read_data();
topological_sort();
getch();
}
void read_data()
{
int i,j;
printf("Enter the number of jobs: \t");
scanf("%d", &n);
printf("Enter the adjacency matrix: \n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
scanf("%d", &a[i][j]);
}
}
void find_indegree()
{
int i,j,sum;
for(j=0;j<n;j++)
{
sum=0;
for(i=0;i<n;i++)
sum=sum+a[i][j];
indegree[j]=sum;
}

}
void topological_sort()
{
int i,k=0,top=-1,u,v,t[10],s[10];
find_indegree();
for(i=0;i<n;i++)
{
if(indegree[i]==0)
s[++top]=i;
}
while(top!=-1)
{
u=s[top--];
t[k++]=u;
for(v=0;v<n;v++)
{
if(a[u][v]==1)
{
indegree[v]--;
if(indegree[v]==0)
s[++top]=v;
}
}
}
printf("\n The sequence is: \t");
for(i=0;i<n;i++)
printf("%d \t", t[i]);
}

10.DIJKSTRAS
#include<stdio.h>
#include<conio.h>
#define infinity 999
void dij(int n, int v, int cost[10][10], int dist[100])
{
int i,u,count,w,flag[10],min;
for(i=1;i<=n;i++)
flag[i]=0,dist[i]=cost[v][i];
count=2;
while(count<=n)
{
min=99;
for(w=1;w<=n;w++)
if(dist[w]<min && !flag[w])
min=dist[w],u=w;
flag[u]=1;
count++;
for(w=1;w
<=n;w++)
if((dist[u]+cost[u][w]<dist[w])&& !flag[w])
dist[w]=dist[u]+cost[u][w];
}
}
void main()
{
int n,v,i,j,cost[10][10],dist[10];
clrscr();
printf("\n Enter the number of nodes: \t");
scanf("%d",&n);
printf("\n Enter the cost matrix: \n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d", &cost[i][j]);

if(cost[i][j]==0)
cost[i][j]=infinity;
}
}
printf("\n Enter the source: ");
scanf("%d",&v);
dij(n,v,cost,dist);
printf("\n Shortest path: \n");
for(i=1;i<=n;i++)
{
if(i!=v)
printf("%d -> %d, cost=%d \n",v,i,dist[i]);
}
getch();
}

11. KRUSKALS
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int
int
int
int

i,j,k,a,b,u,v,n,ne=1;
min,mincost=0,cost[9][9],parent[9];
find(int);
uni(int,int);

void main()
{
clrscr();
printf("\n \n Implementation of Kruskal's Algorithm \n \n");
printf("\n Enter the number of vertices: \t");
scanf("%d",&n);
printf("\n Enter the cost adjacency matrix : \n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d", &cost[i][j]);
if(cost[i][j]==0)
cost[i][j]=999;
}
}
printf("\n The edges of the minimum cost spanning tree are: \n");
while(ne<n)
{
for(i=1,min=999;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(cost[i][j]<min)
{
min=cost[i][j];
a=u=i;
b=v=j;
}

}
}
u=find(u);
v=find(v);
if(uni(u,v))
{
printf("\n %d edge(%d,%d)= %d \n", ne++,a,b,min);
mincost+=min;
}
cost[a][b]=cost[b][a]=999;
}
printf("\n \t Minimum cost is %d \n",mincost);
getch();
}
int find(int i)
{
while(parent[i])
i=parent[i];
return i;
}
int uni(int i, int j)
{
if(i!=j)
{
parent[j]=i;
return 1;
}
return 0;
}

12.FLOYDS
#include<stdio.h>
#include<conio.h>
int n, cost[10][10], d[10][10];
void dist();
void read_data();
void write_data();
int min(int a, int b)
{
return a<b ? a:b;
}
void write_data()
{
int i,j;
printf("The distance matrix is shown below \n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
printf("%d \t", d[i][j]);
printf("\n");
}
}
void read_data()
{
int i,j;
printf("Enter the number of nodes \n");
scanf("%d", &n);
printf("\n Enter the cost adjacency matrix: \n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
scanf("%d",&cost[i][j]);
}
}

void dist()
{
int i,j,k;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
d[i][j]=cost[i][j];
}
for(k=0;k<n;k++)
{
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
d[i][j]=min(d[i][j], d[i][k]+d[k][j]);
}
}
}
void main()
{
clrscr();
read_data();
dist();
write_data();
getch();
}

13. SUM OF SUBSET


#include<stdio.h>
#include<conio.h>
int count,d,w[10],x[10];
void subset(int cs,int k,int r)
{
int i;
x[k]=1;
if(cs+w[k]==d)
{
printf("\nsubset solution=%d\n",++count);
for(i=0;i<=k;i++)
{
if(x[i]==1)
printf("%d ",w[i]);
}
}
else if(cs+w[k]+w[k+1]<=d)
subset(cs+w[k],k+1,r-w[k]);
if((cs+r-w[k]>=d)&&(cs+w[k+1]<=d))
{
x[k]=0;
subset(cs,k+1,r-w[k]);
}
}
void main()
{
int sum=0,i,n;
clrscr();
printf("\nEnter no of elements\n");
scanf("%d",&n);
printf("Enter thr element in asending order");
for(i=0;i<n;i++)
scanf("%d",&w[i]);
printf("\nEnter required sum");
scanf("%d",&d);
for(i=0;i<n;i++)
sum+=w[i];
if(sum<d)
{
printf("No solution");
return;
}
printf("\nSolution is \n");
count=0;
subset(0,0,sum);
getch();
}

15..Horspool method
#include<stdio.h>
#include<conio.h>
#include<string.h>
char p[40],t[40];
int m,n,s[256];
void read_data()
{
printf("Enter the text string\n");
gets(t);
printf("Enter the pattern string\n");
gets(p);
}
void shift_table()
{
int i;
m=strlen(p);
for(i=0;i<128;i++)
s[i]=m;
for(i=0;i<m-1;i++)
s[p[i]]=m-1-i;
}
int pattern_match()
{
int i,k;
shift_table();
m=strlen(p);
n=strlen(t);
i=m-1;
while(i<n)
{
k=0;
while(k<m&&t[i-k]==p[m-1-k])
k++;
if(k==m)
return i-m+1;
i=i+s[t[i]];
}
return -1;
}
void main()
{
int pos;
read_data();
pos=pattern_match();
if(pos==-1)
printf("Pattern string not found");
else
printf("Pattern found at %d position",pos+1);
getch();
}
16. BINOMIAL COEFICIENT

#include<stdio.h>
#include<conio.h>
int binomial_coeff(int n,int k)
{
int i,j,c[10][10];
for(i=0;i<=n;i++)
{
for(j=0;j<n;j++)
if(j==0 || i==j)
c[i][j]=1;
else
c[i][j]=c[i-1][j-1]+c[i-1][j];
}
return(c[n][k]);
}
void main()
{
int n,k,res;
clrscr();
printf("\nEnter the values of n and k");
scanf("%d%d",&n,&k);
res=binomial_coeff(n,k);
printf("\n c(%d,%d) = %d",n,k,res);
getch();
}

17. PRIMS
#include<stdio.h>
#include<conio.h>
int a,b,v,u,n,i,j,ne=1;
int visited[10]={0}, min,mincost=0,cost[10][10];
void main()
{
clrscr();
printf("Enter the number of nodes: \t");
scanf("%d",&n);
printf("\n Enter the adjacency matrix: \n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&cost[i][j]);
if(cost[i][j]==0)
cost[i][j]=999;
}
visited[1]=1;
printf("\n");
}
while(ne<n)
{
for(i=0,min=999;i<=n;i++)
for(j=1;j<=n;j++)
if(cost[i][j]<min)
if(visited[i]!=0)
{
min=cost[i][j];
a=u=i;
b=v=j;
}
if(visited[u]==0 || visited[v]==0)
{
printf("\n Edge %d= (%d %d) \t cost:%d",ne+
+,a,b,min);
mincost+=min;
visited[b]=1;
}

cost[a][b]=cost[b][a]=999;
}
printf("\n Minimum cost is %d", mincost);
getch();
}

18.WARSHALLS
#include<stdio.h>
int n,a[10][10],p[10][10];
void path_matrix();
void read_data();
void write_data();
void write_data()
{
int i,j;
printf("The path matrix is shown below\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
printf("%d ",p[i][j]);
printf("\n");
}
}
void read_data()
{
int i,j;
printf("Enter the number of nodes\n");
scanf("%d",&n);
printf("Enter the adjacency matrix\n");
for (i=0;i<n;i++)
for (j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
void path_matrix()
{
int i,j,k;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
p[i][j]=a[i][j];
}
for(k=0;k<n;k++)
{
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(p[i][k]==1&&p[k][j]==1)
p[i][j]=1;
}
}
}
}
void main()
{

clrscr();
read_data();
path_matrix();
write_data();
getch();
}

Вам также может понравиться