'Pgm:1 (A) BINARY SEARCH
'Pgm:1 (A) BINARY SEARCH
#include<stdio.h>
#include<conio.h>
#include<time.h>
int bin_search(int[],int,int);
int key,a[20],n;
void main()
{
int i,ans,low,high;
clock_t st,end;
clrscr();
printf("Enter the no.of elements\n");
scanf("%d",&n);
printf("Enter the elements in the ascending order\n");
for(i=0; i<n; i++)
scanf("%d",&a[i]);
printf("Enter the key element\n");
scanf("%d",&key);
low=0, high=n-1;
st=clock();
//delay(1000);
ans=bin_search(a,low,high);
end=clock();
if(ans==-1)
{ printf("key not found\n"); }
Else
{ printf("Key found at position %d\n",ans+1);
printf("Time taken to search is %f\n",(end-st)/CLK_TCK); }
getch();
}
OUTPUT
#include<stdio.h>
#include<conio.h>
#include<time.h>
int key;
int lin_search(int a[],int n)
{
if(n<0)
return -1;
if(key==a[n])
return n;
else
return lin_search(a,n-1);
}
void main()
{
int a[100],n,i,res;
clock_t st,end;
clrscr();
printf("Enter the no.of elements\n");
scanf("%d",&n);
printf("Enter the numbers in ascending order\n");
for(i=0; i<n; i++)
scanf("%d",&a[i]);
printf("Enter the key to be searched\n");
scanf("%d",&key);
st=clock();
res=lin_search(a,n-1);
end=clock();
if(res==-1)
printf("Key element not found\n");
else
printf("Key element found at position %d\n",res+1);
printf("Time taken to search is %f\n",(end-st)/CLK_TCK);
getch();
}
OUTPUT
Enter the no.of elements
5
Enter the numbers in ascending order
11 22 33 44 55
Enter the key to be searched
44
Key element found at position 4
Time taken to search is 0.000000
Pgm:2 HEAP SORT
#include<stdio.h>
#include<conio.h>
#include<time.h>
void heap(int a[],int);
void heap_sort(int a[],int n);
void main()
{
int i,n,a[20];
clock_t st,end;
clrscr();
printf("Enter no.of elements\n");
scanf("%d",&n);
printf("Enter array elements\n");
for(i=1; i<=n; i++)
scanf("%d",&a[i]);
printf("Sorted list\n");
st=clock();
heap(a,n);
heap_sort(a,n);
end=clock();
printf("\nTime require for sorting is %f\n",(end-st)/CLK_TCK);
getch();
}
OUTPUT
void main()
{
int a[100],i,n,low,high;
clock_t st,end;
clrscr();
printf("Enter the no,of elements\n");
scanf("%d",&n);
printf("Enter the array elements\n");
for(i=0; i<n; i++)
scanf("%d",&a[i]);
st=clock();
low=0;
high=n-1;
mergesort(a,low,high);
end=clock();
printf("The sorted array is\n");
for(i=0; i<n; i++)
printf("%d\n",a[i]);
printf("Time taken required is %f\n",(end-st)/CLK_TCK);
getch();
}
OUTPUT
1. Enter the no,of elements
10
Enter the array elements
98 23 76 7 34 45 22 11 1 12
The sorted array is
1 7 11 12 22 23 34 45 76 98
Time taken required is 18.901099
void main()
{
int a[100],n,i;
clock_t st,end;
clrscr();
printf("Enter the no.of elements\n");
scanf("%d",&n);
printf("Enter the array elements\n");
for(i=0; i<n; i++)
scanf("%d",&a[i]);
st=clock();
sel(a,n);
end=clock();
printf("The sorted array is\n");
for(i=0; i<n; i++)
printf("%d\n",a[i]);
printf("time taken to sort is %f\n",(end-st)/CLK_TCK);
getch();
}
OUTPUT
Enter the no.of elements
5
Enter the array elements
12 7 20 50 35
The sorted array is
7 12 20 35 50
Time taken to sort is 0.000000
Pgm:5 KNAPSACK PROBLEM
#include<stdio.h>
#include<conio.h>
void main()
{
int m,i,j,n,w[10],p[10],v[10][10];
clrscr();
printf("Enter the no.of objects\n");
scanf("%d",&n);
printf("Enter the weights of n objects\n");
for(i=1; i<=n; i++)
scanf("%d",&w[i]);
printf("Enter the profit of n objects\n");
for(i=1; i<=n; i++)
scanf("%d",&p[i]);
printf("Enter the capacity of knapsack\n");
scanf("%d",&m);
knapsack(n,w,m,p);
getch();
}
OUTPUT
Enter the no.of objects
5
Enter the weights of n objects
32145
Enter the profit of n objects
25 20 15 40 50
Enter the capacity of knapsack
6
The output is
0 0 0 0 0 0 0
0 0 0 25 25 25 25
0 0 20 25 25 45 45
0 15 20 35 40 45 60
0 15 20 35 40 55 60
0 15 20 35 40 55 65
The optimal solution is 65
x[1] x[2] x[3] x[4] x[5] =0 0 1 0 1
Pgm:6 DIJKSTRA'S ALGORITHM
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,a[10][10],s[10],d[10],k,min,u,v,n;
clrscr();
printf("Enter the no of vertices\n");
scanf("%d",&n);
printf("Enter the cost adjacency matrix\n");
for(i=1; i<=n; i++)
{
for(j=1; j<=n; j++)
scanf("%d",&a[i][j]);
}
printf("Enter the source matrix\n");
scanf("%d",&v);
for(i=1; i<=n; i++)
{
s[i]=0, d[i]=a[v][i];
}
d[v]=0, s[v]=1;
for(k=2; k<=n; k++)
{
min=999;
for(i=1; i<=n; i++)
if(s[i]==0 && d[i]<min)
{
min=d[i], u=i;
}
s[u]=1;
for(i=1; i<=n; i++)
if(s[i]==0)
{
if(d[i]>(d[u]+a[u][i]))
d[i]=d[u]+a[u][i];
}
}
printf("The shortest distance from %d is \n",v);
for(i=1; i<=n; i++)
printf("%d--->%d=%d\n",v,i,d[i]);
getch();
}
OUTPUT
Enter the no of vertices
5
Enter the cost adjacency matrix
999 3 999 7 999
3 999 999 5 6
7 2 5 999 4
999 999 6 4 999
999 7 999 3 999
Enter the source matrix
1
The shortest distance from 1 is
1--->1=0
1--->2=3
1--->3=13
1--->4=7
1--->5=9
Pgm:7 QUICKSORT
#include<stdio.h>
#include<conio.h>
#include<time.h>
#include<dos.h>
void main()
{
int i,a[100],n,low,high;clock_t st,end;
clrscr();
printf("Enter no.of elements\n");
scanf("%d",&n);
printf("Enter the array elements\n");
for(i=0; i<n; i++)
scanf("%d",&a[i]);
low=0;
high=n-1;
delay(1000);
st=clock();
quicksort(a,low,high);
end=clock();
printf("Sorted array is\n");
for(i=0; i<n; i++)
printf("%d\n",a[i]);
printf("The time required is %f\n",(end-st)/CLK_TCK);
getch();
}
OUTPUT
Enter no.of elements
5
Enter the array elements
33 11 22 55 44
Sorted array is
11
22
33
44
55
The time required is 2.967033
Pgm:8 KRUSKAL
#include<stdio.h>
#include<conio.h>
#define INF 9999
struct edge
{
int u,v,cost;
};
typedef struct edge EDGE;
void main()
{
int i,j,flag,n,source;
int a[20][20],s[10],t[20][20];
clrscr();
printf("Enter the no.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]);
}
}
printf("Enter rhe source\n");
scanf("%d",&source);
bfs(a,n,source,t,s);
flag=0;
for(i=0; i<n;i++)
{
if(s[i]==0)
{
printf("The vertex %d is not reachable\n",i);
flag=1;
}
else
printf("The vertex %d is reachable\n",i);
}
if(flag==1)
printf("Some nodes are not visible\n");
else
printf("BFS traversal is\n");
for(i=0; i<n; i++)
printf("%d---->%d\n",t[i][0],t[i][1]);
getch();
}
OUTPUT
Enter the no.of nodes
4
Enter the adjacency matrix
0110
0000
0001
0100
Enter rhe source
0
The vertex 0 is reachable
The vertex 1 is reachable
The vertex 2 is reachable
The vertex 3 is reachable
BFS traversal is
0---->1
0---->2
2---->3
0---->0
void main()
{
int n,i,j,cost[10][10],s[10],connected,flag;
clrscr();
printf("Enter the no.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",&cost[i][j]);
}
}
connected=0;
for(j=0; j<n; j++)
{
for(i=0; i<n; i++)
s[i]=0;
dfs(n,cost,j,s);
flag=0;
for(i=0; i<n; i++)
{
if(s[i]==0) flag=1;
}
if(flag==0) connected=1;
}
if(connected==1)
printf("Graph is connected\n");
else
printf("Graph is not connecte\n");
getch();
}
OUTPUT
Enter the no.of nodes
4
Enter the adjacency matrix
0110
0000
0100
0000
Graph is not connected
Pgm:10 SUBSET PROBLEM
#include<stdio.h>
#include<conio.h>
void main()
{
char p[20],t[40];
int pos;
clrscr();
printf("Enter the text string\n");
scanf("%s",&t);
printf("Enter the pattern string\n");
scanf("%s",&p);
pos=horspool(p,t);
if(pos==-1)
printf("Pattern string not found\n");
else
printf("Pattern string found at %dth position\n",pos+1);
getch();
}
OUTPUT
void main()
{ clrscr();
printf("Enter no.of vertices\n");
scanf("%d",&n);
printf("Enter the cost of matrix\n");
for(i=1; i<=n; i++)
{
for(j=1; j<=n; j++)
scanf("%d",&c[i][j]);
visited[i]=0;
}
prims();
getch(); }
void prims()
{
int a,b,k,min=1000,cost=0,count=0;
visited[1]=1;
while(count<n-1)
{
min=999;
for(i=1; i<=n; i++)
{
for(j=1; j<=n; j++)
if(visited[i] && !visited[j] && min>c[i][j])
{
min=c[i][j], a=i, b=j;
}
}
printf("%d--->%d\n",a,b,c[a][b]);
cost+=c[a][b];
visited[b]=1;
count++;
}
printf("Total cost of spanning tree is %d",cost);
}
OUTPUT
Enter no.of vertices
6
Enter the cost of matrix
999 3 999 999 6 5
3 999 1 6 999 4
999 1 999 6 999 4
999 6 6 999 8 5
6 999 999 8 999 2
5 4 4 5 2 999
1--->2=3
2--->3=1
2--->6=4
6--->5=2
6--->4=5
Total cost of spanning tree is 15
Pgm:13 Floyd’s Algorithm
#include<stdio.h>
#include<conio.h>
int min(int a,int b)
{
if(a<b)
return a;
return b;
}
void main()
{
int n,a[10][10],p[10][10];
clrscr();
printf("Enter the no.of vertices\n");
scanf("%d",&n);
printf("Enter the cost of adjacency matrix\n");
read_matrix(n,a);
floyds(n,a,p);
printf("The transition closure is as shown\n");
write_matrix(n,p);
getch();
}
OUTPUT
Enter the no.of vertices
4
Enter the cost of adjacency matrix
0 9999 3 9999
2 0 9999 9999
9999 7 0 1
6 9999 9999 0
The transition closure is as shown
0 10 3 4
2 0 5 6
7 7 0 1
6 16 9 0
Pgm:14 Warshall’s Algorithm
#include<stdio.h>
#include<conio.h>
void main()
{
int n,a[10][10],p[10][10];
clrscr();
printf("Enter the no.of nodes\n");
scanf("%d",&n);
printf("Enter the cost of adjacency matrix\n");
read_matrix(n,a);
warshall(n,a,p);
printf("The transitive closure of (path) matrix is as shown\n");
write_matrix(n,p);
getch();
}
OUTPUT
Enter the no.of nodes
4
Enter the cost of adjacency matrix
0100
0001
0000
1010
The transitive closure of (path) matrix is as shown
1 1 1 1
1 1 1 1
0 0 0 0
1 1 1 1
Pgm:15 N-Queen’s Problem
#include<stdio.h>
#include<conio.h>
#include<math.h>
int count,x[10];
void nqueens(int,int);
void display(int,int x[]);
int place(int,int);
void main()
{
int n;
clrscr();
printf("Enter the no.of queens\n");
scanf("%d",&n);
nqueens(1,n);
if(count==0)
printf("No possible solution\n");
else
printf("No.of solutions are %d ",count);
getch();
}