Ada Op2
Ada Op2
#include<stdio.h>
#include<time.h>
#include<conio.h>
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 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();
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();
for(i=0;i<n;i++)
printf("%d\t",a[i]);
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];
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];
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();
scanf("%d",&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;
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;
for(i=0;i<n;i++)
printf("%d",t[i]);
void main()
int i,j;
clrscr();
scanf("%d",&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;
#include<stdio.h>
#include<conio.h>
int sum=0;
if(a>b)
return a;
else
return b;
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--;
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);
void main()
int w[10],p[10],i,m,n;
scanf("%d",&n);
for(i=1;i<n;i++)
scanf("%d",&w[i]);
for(i=1;i<n;i++)
scanf("%d",&p[i]);
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++)
q[++r]=i;
if(f<=r)
visited[q[f]]=1;
bfs(q[f++]);
void main()
int v;
clrscr();
scanf("%d",&n);
for(i=1;i<=n;i++)
q[i]=0;
visited[i]=0;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
scanf("%d",&v);
bfs(v);
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();
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)
else
getch();
}
Program-8[binary search]
#include<stdio.h>
#include<conio.h>
#define SIZE 10
int n;
void main()
int A[SIZE],KEY,i,flag;
clrscr();
scanf("%d",&n);
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)
else
getch();
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;
clrscr();
scanf("%d",&n);
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;
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()
scanf("%d",&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;
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()
scanf("%d",&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;
getch();
Output: