0% found this document useful (0 votes)
123 views

DAA Lab Programs

The document discusses various sorting and graph algorithms: 1. It provides code implementations for merge sort, quick sort, and binary search sorting algorithms. 2. It also includes code for Kruskal's and Prim's minimum spanning tree algorithms to find minimum cost trees for weighted undirected graphs. 3. Dynamic programming solutions are given for the 0/1 knapsack problem and making change problem (coin changing problem). 4. Brief mentions are made of breadth-first search and depth-first search graph algorithms but no code is shown.

Uploaded by

harishpillai1994
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
123 views

DAA Lab Programs

The document discusses various sorting and graph algorithms: 1. It provides code implementations for merge sort, quick sort, and binary search sorting algorithms. 2. It also includes code for Kruskal's and Prim's minimum spanning tree algorithms to find minimum cost trees for weighted undirected graphs. 3. Dynamic programming solutions are given for the 0/1 knapsack problem and making change problem (coin changing problem). 4. Brief mentions are made of breadth-first search and depth-first search graph algorithms but no code is shown.

Uploaded by

harishpillai1994
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

1-Merge Sort

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

void marge(int a[],int lb,int mid,int ub)
{
int i,j,k,b[10];
i=lb;
j=mid+1;
k=lb;
while(i<=mid && j<=ub)
{
if(a[i]<a[j])
{
b[k]=a[i];
i++;
}
else if(a[i]>a[j])
{
b[k]=a[j];
j++;
}
else{
b[k++]=a[i];
b[k]=a[j];
i++;
j++;
}
k++;
}
while(i<=mid)
{
b[k]=a[i];
k++;
i++;
}
while(j<=ub)
{
b[k]=a[j];
j++;
k++;
}
k=lb;
while(k<=ub)
{
a[k]=b[k];
k++;
}
return;
}
void devide(int a[],int l,int u)
{
int m=(l+u)/2;
if(l<u)
{
devide(a,l,m);
devide(a,m+1,u);
}
marge(a,l,m,u);
return;
}
void main()
{
int a[10],i,no;
clrscr();
printf("\n\n Enter no of elements..");
scanf("%d",&no);
printf("\n\n Enter the nos..");
for(i=0;i<no;i++)
scanf("%d",&a[i]);
devide(a,0,no);
printf("\n\n Sorted array is..");
for(i=0;i<no;i++)
printf("%d\t",a[i]);
getch();
}

2-Quick Sort
#include<stdio.h>
#include<conio.h>

void quicksort(int [10],int,int);

int main(){
int x[20],size,i;

printf("Quick Sort Program in C\n\n");
printf("Enter size of the array: ");
scanf("%d",&size);

printf("\nEnter %d values: ",size);
for(i=0;i<size;i++)
scanf("%d",&x[i]);

quicksort(x,0,size-1);

printf("\nSorted values: ");
for(i=0;i<size;i++)
printf(" %d",x[i]);

getch();
return 0;
}

void quicksort(int x[10],int first,int last){
int pivot,j,temp,i;

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];
x[pivot]=x[j];
x[j]=temp;
quicksort(x,first,j-1);
quicksort(x,j+1,last);

}
}

3-Binary Search

#include <stdio.h>
#include<conio.h>
void main()
{
int c, first, last, middle, n, search, array[100];
printf("Enter number of elements\n");
scanf("%d",&n);
printf("Enter %d integers\n", n);
for ( c = 0 ; c < n ; c++ )
scanf("%d",&array[c]);
printf("Enter value to find\n");
scanf("%d",&search);
first = 0;
last = n - 1;
middle = (first+last)/2;
while( first <= last )
{
if ( array[middle] < search )
first = middle + 1;
else if ( array[middle] == search )
{
printf("%d found at location %d.\n", search, middle+1);
break;
}
else
last = middle - 1;
middle = (first + last)/2;
}
if ( first > last )
printf("Not found! %d is not present in the list.\n", search);
getch();
}

4-Kruskals Algo
#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;
}
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;
}
5-Prims Algo
#include<stdio.h>
#include<conio.h>
int a,b,u,v,n,i,j,ne=1;
int visited[10]={0},min,mincost=0,cost[10][10];
void main()
{
clrscr();
printf("\n Enter the number of nodes:");
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=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) cost:%d",ne++,a,b,min);
mincost+=min;
visited[b]=1;
}
cost[a][b]=cost[b][a]=999;
}
printf("\n Minimun cost=%d",mincost);
getch();
}
6-0/1 Knapsak using DP

7-Making Change using DP
#include<stdio.h>
#include<conio.h>
#define COIN 10
#define AMOUNT 10
void main()
{
int c[COIN][AMOUNT]={0},d[COIN]={0};
int n,N,i,j;
printf("Enter the number of Coins : ");
scanf("%d",&n);
printf("Enter Amount : ");
scanf("%d",&N);
printf("\nEnter values for Coins : \n");
for(i=0;i<n;i++)
scanf("%d",&d[i]);
for(i=0;i<n;i++)
{
for(j=0;j<N;j++)
c[i][j]=0;
}
for(i=1;i<n;i++)
c[i][0]=0;
for(i=1;i<=n;i++)
{
for(j=1;j<=N;j++)
{
if(i==1 && j<d[i])
c[i][j]=-1;
else if(i==1)
c[i][j]=1+c[1][j-d[1]];
else if(j<d[i])
c[i][j]=c[i-1][j];
else
{
if(c[i-1][j]<(1+c[i][j-d[i]]))
c[i][j]=c[i-1][j];
else
c[i][j]=1+(c[i][j-d[i]]);
}
}}
for(i=0;i<=N;i++)
{
printf("\t %d",i);
}
printf("\n________________________________________\n");
for(i=0;i<n;i++)
{
printf("%d\t|",d[i]);
for(j=0;j<=N;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
getch();
}

8-BFS

9-DFS

You might also like