0% found this document useful (0 votes)
19 views29 pages

Ada Op2

Uploaded by

1by20is415
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)
19 views29 pages

Ada Op2

Uploaded by

1by20is415
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/ 29

program-1: (quick_sort).

#include<stdio.h>

#include<time.h>

#include<conio.h>

int quick_sort(int x[],int low,int high)

int pos;

if(low<high)

pos=partition(x,low,high);

quick_sort(x,low,pos-1);

quick_sort(x,pos+1,high);

return 0;

int partition(int x[],int low,int high)

int key,temp,true=1;

int left,right;

key=x[low];

left=low+1;

right=high;

while(true)

while((left<high)&&(key>=x[left]))

left++; hi.
delay(1);

while(key<x[right])

{Up

right--;

delay(100);

if(left<right)

temp=x[left];

x[left]=x[right];

x[right]=temp;

else

temp=x[low];

x[low]=x[right];

x[right]=temp;

return(right);

return 0;

void main()

int a[1000],n,i,low,high;

clock_t s,e;
float time;

clrscr();

printf("enter array size\n:");

scanf("%d",&n);

printf("\nelemnets are:");

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

a[i]=(int)rand()%100;

printf("%d\t",a[i]);

low=0;

high=n-1;

s=clock();

quick_sort(a,low,high);

e=clock();

printf("the sorted list is\n");

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

printf("%d\t",a[i]);

printf("\n time taken =%f",(e-s)/CLK_TCK);

getch();

}
output:

program-2:( merge_sort)

#include<stdio.h>

#include<conio.h>

#include<time.h>
void merge(int a[],int low,int mid,int high)

int i,j,k,b[20];

i=low; j=mid+1; k=low;

while(i<=mid && j<=high)

if(a[i]<=a[j])

b[k++]=a[i++];

delay(1);

else

b[k++]=a[j++];

delay(100);

while(i<=mid) b[k++]=a[i++];

while(j<=high) b[k++]=a[j++];

for(k=low; k<=high;k++)

a[k]=b[k];

void mergesort(int a[],int low,int high)

int mid;

if(low>=high)

return;
mid=(low+high)/2;

mergesort(a,low,mid);

mergesort(a,mid+1,high);

merge(a,low,mid,high);

void main()

int n,a[2000],k;

clock_t st,et;

double ts;

clrscr();

printf("enetr how many no:");

scanf("%d",&n);

printf("the random no are:\n");

for(k=1;k<=n;k++)

a[k]=rand();

printf("%d\t",a[k]);

st=clock();

mergesort(a,1,n);

et=clock();

ts=(double)(et-st)/CLOCKS_PER_SEC;

printf("\n sorted no are:\n");

for(k=1;k<=n;k++)

printf("%d\t",a[k]);
printf("\n the time taken is %f ",ts);

getch();

output:
program-3:(topological ordering)

#include<stdio.h>

#include<conio.h>

int a[10][10],n,indegre[10];

void find_indegre()

int j,i,sum;

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

sum=0;

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

sum+=a[i][j];

indegre[j]=sum;

void topology()

int i,u,v,t[10],s[10],top=-1,k=0;

find_indegre();

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

if(indegre[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)

indegre[v]--;

if(indegre[v]==0) s[++top]=v;

printf("the topological sequence is:\n");

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

printf("%d",t[i]);

void main()

int i,j;

clrscr();

printf("enter the number of jobs:");

scanf("%d",&n);

printf("\n enter the adjacency matrix:\n");

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

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

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

topology();
getch();

output:

Proram-4:(TSP)

#include<stdio.h>
#include<conio.h>
int matrix[25][25],visited_cities[10],limit,cost=0;
int tsp(int c)
{
int count,nearest_city=999;
int minimum=999,temp;
for(count=0;count<limit;count++)
{
if((matrix[c][count]!=0)&&(visited_cities[count]==0))
{
if(matrix[c][count]<minimum)
{
minimum=matrix[count][0]+matrix[c][count];
}
temp=matrix[c][count];
nearest_city=count;
}
}
if(minimum!=999)
{cost=cost+temp;
}
return nearest_city;
}
void minimum_cost(int city)
{
int nearest_city;
visited_cities[city]=1;
printf("%d",city+1);
nearest_city=tsp(city);
if(nearest_city==999)
{
nearest_city=0;
printf("%d",nearest_city+1);
cost=cost+matrix[city][nearest_city];
return;
}
minimum_cost(nearest_city);
}
int main()
{
int i,j;

printf("enter total number of cities:\t");


scanf("%d",&limit);
for(i=0;i<limit;i++)
{
printf("\n enter %d elements in row[%d]\n",limit,i+1);
for(j=0;j<limit;j++)
{
scanf("%d",&matrix[i][j]);
}
visited_cities[i]=0;
}
printf("\n entered cost matrix\n");
for(i=0;i<limit;i++)
{
printf("\n");
for(j=0;j<limit;j++)
{
printf("%d",matrix[i][j]);
}}
printf("\n\n path:\t");
minimum_cost(0);
printf("\n\n minimum cost:\t");
printf("%d\n",cost);
return 0;
}
Output:
Program-5:(knapsack)

#include<stdio.h>

#include<conio.h>

int sum=0;

int max(int a,int b)

if(a>b)

return a;

else

return b;

void knapsack(int m,int n,int w[],int p[])

int v[100][200],x[10],i,j;

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

v[0][i]=0;
for(i=1;i<=n;i++

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

if(j>=w[i])

v[i][j]=max(v[i-1][j],v[i-1][j-w[1]]+p[i]);

else

v[i][j]=v[i-1][j];

for(i=1;i<=n;i++)

x[i]=0;

i=n;

j=m;

while(i>0&&j>0)

if(v[i][j]!=v[i-1][j])

x[i]=1;

j=j-w[i];

i--;

printf("\n the optimal set of weights is:\n");

for(i=1;i<=n;i++)

if(x[i]==1)
{

printf("X%d=1\t",i);

sum=sum+p[i];

else

printf("X%d=0\t",i);

printf("\n total profit=%d",sum);

void main()

int w[10],p[10],i,m,n;

printf("\t0/1 knapsack problem\n\n");

printf("enter the number of items:");

scanf("%d",&n);

printf("enter the weights of the items:\n");

for(i=1;i<n;i++)

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

printf("enter the profits of the items:\n");

for(i=1;i<n;i++)

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

printf("enter the capacity of knapsach:");

scanf("%d",&m);

knapsack(m,n,w,p);

getch();

}
Output:

Program-6:(BFS)

#include<stdio.h>

#include<conio.h>

int a[20][20],q[20],visited[20],n,i,j,f=0,r=-1;

void bfs(int v)

for(i=1;i<=n;i++)

if(a[v][i] && !visited[i])

q[++r]=i;

if(f<=r)

visited[q[f]]=1;
bfs(q[f++]);

void main()

int v;

clrscr();

printf("\n enter the number of vertices");

scanf("%d",&n);

for(i=1;i<=n;i++)

q[i]=0;

visited[i]=0;

printf("\n enter graph data in matrix form:\n");

for(i=1;i<=n;i++)

for(j=1;j<=n;j++)

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

printf("\n enter the starting vertix:");

scanf("%d",&v);

bfs(v);

printf("\n the node which are reachable are :\n");

for(i=1;i<=n;i++)

if(visited[i])

printf("%d\t",i);

getch();

}
Output:
Program-7(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=0;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 the number of vertices;");

scanf("%d",&n);

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

reach[i]=0;

for(j=1;j<=n;j++)

a[i][j]=0;

}
printf("\n enter the adjacency matrix:\n");

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();

}
Program-8[binary search]

#include<stdio.h>

#include<conio.h>

#define SIZE 10

int n;

void main()

int A[SIZE],KEY,i,flag;

intBinSearch(int A[SIZE],int KEY);

clrscr();

printf("how many elements in an array?");

scanf("%d",&n);

printf("enetr the elements");

for(i=0;i,n;i++)

scanf("%d",&A[i]);
printf("\n enter the elements which is to be searched");

scanf("%d",&KEY);

flag=BinSearch(A,KEY);

if(flag==-1)

printf("\n the element is not present");

else

printf("\n the element is at A[%d] location",flag);

getch();

intBinSearch(int A[SIZE], int KEY)

intlow,high,m;

low=0;

high=n-1;

while(low<=high)

m=(low+high)/2;

if(KEY==A[m])

return m;

else if(KEY<A[m])

high=m-1;

else

low=m+1;

return -1;

Output:
Program-9(insertion sort)

#include<stdio.h>#include<conio.h>

void main()

int A[10],n,i;

voidInsert_sort(int A[10],int n);

clrscr();

printf("\n \t\tInsertion Sort");

printf("\n how many elements are there?");

scanf("%d",&n);

printf("\n enter the elements\n");


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

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

Insert_sort(A,n);

getch();

voidInsert_sort(int A[10],int n)

inti,j,temp;

for(i=1;i<=n-1;i++)

temp=A[i];

j=i-1;

while((j>=0)&&(A[j]>temp))

A[j+1]=A[i];

j=j-1;

A[j+1]=temp;

printf("\n the sorted list of elements is..\n");

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

printf("\n %d",A[i]);

}
Output:

Program-10(prim’s algorithm)

#include<stdio.h>

#include<conio.h>

int a,b,u,v,i,j,n,ne=1;

int visited[10]={0},min,mincost=0,cost[10][10];
void main()

printf("\n enter the number of nodes:");

scanf("%d",&n);

printf("enetr 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=1,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)cos%d",ne++,a,b,min);

mincost=min;

visited[b]=1;

cost[a][b]=cost[b][a]=999;

printf("\n minimum cost=%d",mincost);

getch();

Output:

#include<stdio.h>

#include<conio.h>

int a,b,u,v,i,j,n,ne=1;

int visited[10]={0},min,mincost=0,cost[10][10];

void main()

printf("\n enter the number of nodes:");

scanf("%d",&n);

printf("enetr 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=1,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)cos%d",ne++,a,b,min);

mincost=min;

visited[b]=1;

cost[a][b]=cost[b][a]=999;

printf("\n minimum cost=%d",mincost);

getch();

Output:

You might also like