Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

DAA Lab Programms

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 19

RAJ KUMAR GOEL INSTITUTE OF TECHNOLOGY AND

MANAGEMENT, GHAZIABAD

Affiliated to Dr. A.P.J. Abdul kalam Technical


University, Lucknow

DAA LAB
(KCS-553)

Session: 2023-24

Submitted by: Submitted to:


Vashu Gupta Ms. Kanupriya Singh
(2103330100089) (Assistant Professor)
Deptt. Of CS
Prog.No. List of Programs

1 Write a Program for Linear search in C.

2 Write a Program for Binary search in C.

3 Write a Program for Insertion Sort in C.

4 Write a Program for Selection Sort in C.

5 Write a Program for Quick Sort in C.

6 Write a Program for Merge Sort in C.

7 Write a Program for Merge Sort in C.

8 Write a Program to Implement Kruskal’s Algorithm in C.

9 Write a Program to Implement Floyd Warshall’s algorithm in C.

10 Write a Program to Implement Floyd Warshall’s algorithm in C.

2
1.Write a Program for Linear search in C.
#include <stdio.h>
int main()
{
int array[100], search, c, n;

printf("Enter the number of elements in array\n");


scanf("%d",&n);

printf("Enter %d integer(s)\n", n);

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


scanf("%d", &array[c]);

printf("Enter the number to search\n");


scanf("%d", &search);

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


{
if (array[c] == search) /* if required element found */
{
printf("%d is present at location %d.\n", search, c+1);
break;
}
}
if (c == n)
printf("%d is not present in array.\n", search);

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];

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

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;

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]);
}

for (c = 1 ; c <= n - 1; c++) {


d = c;

while ( d > 0 && array[d] < array[d-1]) {


t = array[d];
array[d] = array[d-1];
array[d-1] = t;

d--;
}
}

printf("Sorted list in ascending order:\n");

for (c = 0; c <= n - 1; c++) {


printf("%d\n", array[c]);
}

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;

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

for ( c = 0 ; c < ( n - 1 ) ; c++ )


{
position = c;

for ( d = c + 1 ; d < n ; d++ )


{
if ( array[position] > array[d] )
position = d;
}
if ( position != c )
{
swap = array[c];
array[c] = array[position];
array[position] = swap;
}
}

printf("Sorted list in ascending order:\n");

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


printf("%d\n", array[c]);

return 0;
}

7
5.Write a Program for Quick Sort in C.
#include<stdio.h>

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

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

printf("Enter size of the array: ");


scanf("%d",&size);

printf("Enter %d elements: ",size);


for(i=0;i<size;i++)
scanf("%d",&x[i]);

quicksort(x,0,size-1);

printf("Sorted elements: ");


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

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];

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

void mergeSort(int arr[],int low,int mid,int high);


void partition(int arr[],int low,int high);

int main(){

int merge[MAX],i,n;

printf("Enter the total number of elements: ");


scanf("%d",&n);

printf("Enter the elements which to be sort: ");


for(i=0;i<n;i++){
scanf("%d",&merge[i]);
}

partition(merge,0,n-1);

printf("After merge sorting elements are: ");


for(i=0;i<n;i++){
printf("%d ",merge[i]);
}

return 0;
}

void partition(int arr[],int low,int high){

int mid;

if(low<high){
mid=(low+high)/2;
partition(arr,low,mid);
partition(arr,mid+1,high);
mergeSort(arr,low,mid,high);
}
}

void mergeSort(int arr[],int low,int mid,int 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

You might also like