DAA Lab Programms
DAA Lab Programms
DAA Lab Programms
MANAGEMENT, GHAZIABAD
DAA LAB
(KCS-553)
Session: 2023-24
2
1.Write a Program for Linear search in C.
#include <stdio.h>
int main()
{
int array[100], search, c, n;
return 0;
}
OUTPUT
3
2.Write a Program for Binary search in C.
#include <stdio.h>
int main()
{
int c, first, last, middle, n, search, array[100];
first = 0;
last = n - 1;
middle = (first+last)/2;
return 0;
}
4
Output
5
3.Write a Program for Insertion Sort in C.
#include <stdio.h>
int main()
{
int n, array[1000], c, d, t;
d--;
}
}
return 0;
}
6
4.Write a Program for Selection Sort in C.
#include <stdio.h>
int main()
{
int array[100], n, c, d, position, swap;
return 0;
}
7
5.Write a Program for Quick Sort in C.
#include<stdio.h>
int main(){
int x[20],size,i;
quicksort(x,0,size-1);
return 0;
}
if(first<last){
pivot=first;
i=first;
j=last;
while(i<j){
while(x[i]<=x[pivot]&&i<last)
i++;
while(x[j]>x[pivot])
j--;
if(i<j){
temp=x[i];
x[i]=x[j];
x[j]=temp;
}
}
temp=x[pivot];
8
x[pivot]=x[j];
x[j]=temp;
quicksort(x,first,j-1);
quicksort(x,j+1,last);
}
}
9
6.Write a Program for Merge Sort in C.
#include<stdio.h>
#define MAX 50
int main(){
int merge[MAX],i,n;
partition(merge,0,n-1);
return 0;
}
int mid;
if(low<high){
mid=(low+high)/2;
partition(arr,low,mid);
partition(arr,mid+1,high);
mergeSort(arr,low,mid,high);
}
}
int i,m,k,l,temp[MAX];
10
l=low;
i=low;
m=mid+1;
while((l<=mid)&&(m<=high)){
if(arr[l]<=arr[m]){
temp[i]=arr[l];
l++;
}
else{
temp[i]=arr[m];
m++;
}
i++;
}
if(l>mid){
for(k=m;k<=high;k++){
temp[i]=arr[k];
i++;
}
}
else{
for(k=l;k<=mid;k++){
temp[i]=arr[k];
i++;
}
}
for(k=low;k<=high;k++){
arr[k]=temp[k];
}
}
11
7. Write a Program to Implement knapsack problem in C.
# include<stdio.h>
# include<conio.h>
void knapsack(int n, float weight[], float profit[], float capacity)
{
float x[20], tp= 0;
int i, j, u;
u=capacity;
for (i=0;i<n;i++)
x[i]=0.0;
for (i=0;i<n;i++)
{
if(weight[i]>u)
break;
else
{
x[i]=1.0;
tp= tp+profit[i];
u=u-weight[i];
}
}
if(i<n)
x[i]=u/weight[i];
tp= tp + (x[i]*profit[i]);
printf("n The result vector is:- ");
for(i=0;i<n;i++)
printf("%ft",x[i]);
printf("m Maximum profit is:- %f", tp);
}
void main()
{
float weight[20], profit[20], capacity;
int n, i ,j;
float ratio[20], temp;
clrscr();
printf ("n Enter the no. of objects:- ");
scanf ("%d", &num);
printf ("n Enter the wts and profits of each object:- ");
for (i=0; i<n; i++)
{
scanf("%f %f", &weight[i], &profit[i]);
}
printf ("n enter the capacityacity of knapsack:- ");
scanf ("%f", &capacity);
12
for (i=0; i<n; i++)
{
ratio[i]=profit[i]/weight[i];
}
for(i=0; i<n; i++)
{
for(j=i+1;j< n; j++)
{
if(ratio[i]<ratio[j])
{
temp= ratio[j];
ratio[j]= ratio[i];
ratio[i]= temp;
temp= weight[j];
weight[j]= weight[i];
weight[i]= temp;
temp= profit[j];
profit[j]= profit[i];
profit[i]= temp;
}
}
}
knapsack(n, weight, profit, capacity);
getch();
}
13
8.Write a Program to Implement Kruskal’s Algorithm in C.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int i,j,k,a,b,u,v,n,ne=1;
int min,mincost=0,cost[9][9],parent[9];
int find(int);
int uni(int,int);
void main()
{
clrscr();
printf("\n\n\tImplementation of Kruskal's algorithm\n\n");
printf("\nEnter the no. of vertices\n");
scanf("%d",&n);
printf("\nEnter 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("\nThe edges of Minimum Cost Spanning Tree are\n\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;
14
}
cost[a][b]=cost[b][a]=999;
}
printf("\n\tMinimum cost = %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;
}
15
9.Write a Program to Implement Floyd Warshall’s algorithm in C.
#include<Stdio.h>
#include<Conio.h>
int max(int,int);
void warshal(int p[10][10],int n)
{
int i,j,k;
for(k=1;k<=n;k++)
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
p[i][j]=max(p[i][j],p[i][k]&&p[k][j]);
}
int max(int a,int b)
{
if(a>b)
return(a);
else
return(b);
}
void main()
{
int p[10][10]={0},n,e,u,v,i,j;
clrscr();
printf("\n Enter the number of verices:");
scanf("%d",&n);
printf("\n Enter the number of edges:");
scanf("%d",&e);
for(i=1;i<=e;i++)
{
printf("\n Enter the end vertices of edges%d:",i);
scanf("%d%d",&u,&v);
p[u][v]=1;
}
printf("\n Matrix of Input Data:\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%d\t",p[i][j]);
printf("\n");
16
}
warshal(p,n);
printf("\n Traversitive Closure:\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%d\t",p[i][j]);
printf("\n");
}
getch();
}
17
10.Write a Program to Implement Dijakstra’s algorithm in C.
#include<Stdio.h>
#include<Conio.h>
int n;
int weight[100][100];
char intree[100];
int d[100];
int whoTo[100];
void updatedistance(int target)
{
int i;
for(i=0;i<n;i++)
if((weight[target][i]!=0)&&(d[i]>weight[target][i]))
{
d[i]=weight[target][i];
whoTo[i]=target;
}
}
void main()
{
FILE *f=fopen("dist.txt","r");
int i,j,total,treesize;
clrscr();
fscanf(f,"%d",&n);
for(i=0;i
for(j=0;j
fscanf(f,"%d",&weight[i][j]);
fclose(f);
for(i=0;i
intree[i]=0;
printf("Adding Nodes:%c\n",0+'A');
intree[0]=1;
updatedistance(0);
total=1;
for(treesize=1;treesize
{
int min=-1;
for(i=0;i
if(!intree[i])
18
if((min==-1)||(d[min]>d[i]))
min=i;
printf("Adding Edge%c-%c\n",whoTo[min]+'A',min+'A');
intree[min]=1;
total+=d[min];
updatedistance(min);
}
printf("Total Distance:%d \n",total);
getch();
}
19