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

ADA Continue

Uploaded by

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

ADA Continue

Uploaded by

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

INDEX

SR
PAGE
NO. PROGRAM TITLES DATE SIGNATURE REMARKS
NO.

Implementation and Time Complexity


1 of factorial program using iterative and
recursive method

Implementation and Time Complexity


2
of linear and binary search algorithm.

Implementation and Time Complexity


of sorting algorithms.
3
Bubble sort, Selection sort, Insertion
sort, Merge sort and Quicksort

Implementation of a knapsack problem


4
using greedy algorithm

Implementation of a knapsack problem


5
using dynamic programming.

Implementation of chain matrix


6 multiplication using dynamic
programming.

Implementation of making a coin


7
change problem using dynamic
programming

Implementation of Graph and


8
Searching (DFS and BFS).

9 Implement Prim’s algorithm

10 Implement Kruskal’s algorithm.

11 Implement LCS problem.

Implementation of max-heap sort


12
algorithm
Practical – 3
I. Bubble Sort
Algorithm:
//Problem Description: This algorithm sorts an array a using bubble sort.
//Input: The array a of size n.
//Output: Sorted array a.
for i in range (n) do
for j in range (a - i - 1) do
if a[j] > a[j+1]
swap (a[j], a[j+1])
print (a)

Code:
#include<stdio.h>
int main ()
{
int a[10], i, j;
printf (“Enter 10 Numbers\n”);
for (i = 0; i < 10; i++)
{
scanf ("%d",&a[i]);
}
for (i = 0; i < 10; i++)
{
for (j = 0; j < 10 - i - 1; j++)
{
if (a[j] > a[j+1])
{
int t = a[j];
a[j] = a[j+1];
a[j+1] = t;

ENROLLMENT NO.:221420107010
}
}
}
printf (“Sorted Array:\n”);
for (i = 0; i < 10; i++)
{
printf ("%d ",a[i]);
}
}

Output:
Enter 10 Numbers
6
54
77
44
78
12
86
34
22
45
Sorted Array:
6 12 22 34 44 45 54 77 78 86

Time Complexity:
The Time Complexity of Bubble Sort is O (n2).

ENROLLMENT NO.:221420107010
Practical – 3
II. Selection Sort
Algorithm:
//Problem Description: This algorithm sorts an array a using selection sort.
//Input: The array a of size n.
//Output: Sorted array a.
for i in range (n) do
min = i
for j in range (i + 1,n) do
if a[j] < a[min]
j = min
if min != i
swap (a[i], a[min])
print (a)

Code:
#include<stdio.h>
int main ()
{
int a[10], i, j, min;
printf (“Enter 10 Numbers\n”);
for (i = 0; i < 10; i++)
{
scanf ("%d",&a[i]);
}
for (i = 0; i < 10; i++)
{
min = i;
for (j = i + 1; j < 10; j++)
{
if (a[j] < a[min])

ENROLLMENT NO.:221420107010
min = j;
if (min != i)
{
int t = a[min];
a[min] = a[i];
a[i] = t;
}
}
}
printf (“Sorted Array:\n”);
for (i = 0; i < 10; i++)
{
printf ("%d ",a[i]);
}
}

Output:
Enter 10 Numbers
12
34
13
54
22
14
44
56
32
66
Sorted Array:
12 13 14 22 32 34 44 54 56 66
Time Complexity:
The Time Complexity of Selection Sort is O (n2).

ENROLLMENT NO.:221420107010
Practical – 3
III. Insertion Sort
Algorithm:
//Problem Description: This algorithm sorts an array a using insertion sort.
//Input: The array a of size n.
//Output: Sorted array a.
for i = 1 in range (n) do
key = a[i]
j=i-1
while j > 0 and a[j] > key
a[j+1] = a[j]
j=j-1
a[j+1] = key
print (a)

Code:
#include<stdio.h>
int main ()
{
int a[10], i, j, key;
printf (“Enter 10 Numbers\n”);
for (i = 0; i < 10; i++)
{
scanf ("%d",&a[i]);
}
for (i = 0; i < 10; i++)
{
key = a[i];
j = i - 1;
while (j > 0 && a[j] > key)
{

ENROLLMENT NO.:221420107010
a[j+1] = a[j];
j--;
}
a[j+1] = key;
}
printf (“Sorted Array:\n”);
for (i = 0; i < 10; i++)
{
printf ("%d ",a[i]);
}
}

Output:
Enter 10 Numbers
12
45
66
44
79
8
54
65
34
77
Sorted Array:
8 12 34 44 45 54 65 66 77 79

Time Complexity:
The Time Complexity of Insertion Sort is O (n2).

ENROLLMENT NO.:221420107010
Practical – 3
IV. Merge Sort
Algorithm:
//Problem Description: This algorithm sorts an array a using merge sort.
//Input: The array a of size n.
//Output: Sorted array a.

merge (a, f, m, r)
{
p=m-f+1
q=r–m
arrays b[p], c[q]
for i in range (p)
b[i] = a[f + i]
for i in range (q)
c[i] = a[m + 1 + i]
i = 0, j = 0, k = f
while i < p && j < q
if (b[i] <= c[j])
a[k++] = b[i++];
else
a[k++] = c[j++];
while i < p
a[k++] = b[i++];
while j < q
a[k++] = c[j++];
}

ENROLLMENT NO.:221420107010
mergeSort (a, f, r)
{
if f < r
m = (f + r) / 2;
mergeSort (a, f, m);
mergeSort (a, m + 1, r);
merge (a, f, m, r);
}
print (a)

Code:
#include<stdio.h>
void merge (int a[], int f, int m, int r)
{
int i, j, k;
int p = m - f + 1;
int q = r – m;
int b[p], c[q];
for (i = 0; i < p; i++)
b[i] = a[f + i];
for (i = 0; i < q; i++)
c[i] = a[m + 1 + i];
i = 0, j = 0; k = f;
while (i < p && j < q)
{
if (b[i] <= c[j])
a[k++] = b[i++];
else
a[k++] = c[j++];
}
while (i < p)
a[k++] = b[i++];

ENROLLMENT NO.:221420107010
while (j < q)
a[k++] = c[j++];
}

void mergeSort (int a[], int f, int r)


{
if (f < r)
{
int m = (f + r) / 2;
mergeSort (a, f, m);
mergeSort (a, m + 1, r);
merge (a, f, m, r);
}
}

int main ()
{
int a[10], i;
printf (“Enter 10 Numbers\n”);
for (i = 0; i < 10; i++)
{
scanf ("%d",&a[i]);
}
mergeSort (a, 0, 9);
printf (“Sorted Array:\n”);
for (i = 0; i < 10; i++)
{
printf ("%d ",a[i]);
}
}

ENROLLMENT NO.:221420107010
Output:
Enter 10 Numbers
12
34
13
54
22
14
44
56
32
66
Sorted Array:
12 13 14 22 32 34 44 54 56 66

Time Complexity:
The Time Complexity of Merge Sort is O (n log n).

ENROLLMENT NO.:221420107010
Practical – 3
V. Quick Sort
Algorithm:
//Problem Description: This algorithm sorts an array a using quick sort.
//Input: The array a of size n.
//Output: Sorted array a.
partition (a, s, b)
{
p = a[b]
i=s-1
for j = s in range (b)
if a[j] < p
i++
swap (a[i], a[j])
swap (a[i + 1], a[b])
return i + 1
}

quickSort (a, s, b)
{
if s < b
p = partition (a, s, b)
quickSort (a, s, p - 1)
quicksort (a, p + 1, b)
}
print (a)

ENROLLMENT NO.:221420107010
Code:
#include<stdio.h>
int partition (int a[], int s, int b)
{
int p = a[b], i = s - 1, j;
for (j = s; j <= b – 1; j++)
{
if (a[j] < p)
{
i++;
int t = a[i];
a[i] = a[j];
a[j] = t;
}
}
int t = a[i + 1];
a[i + 1] = a[b];
a[b] = t;
return i + 1;
}

void quickSort (int a[], int s, int b)


{
if (s < b)
{
int p = partition (a, s, b);
quickSort (a, s, p - 1);
quickSort (a, p + 1, b);
}
}

int main ()

ENROLLMENT NO.:221420107010
{
int a[10], i;
printf (“Enter 10 Numbers\n”);for (i = 0; i < 10;
i++)
{
scanf ("%d",&a[i]);
}
quickSort (a, 0, 9);
printf (“Sorted Array:\n”);for (i = 0; i < 10;
i++)
{
printf ("%d ",a[i]);
}
}

Output:
Enter 10 Numbers
56
97
65
3
23
66
22
89
43
75
Sorted Array:
3 22 23 43 56 65 66 75 89 97

Time Complexity:
The Time Complexity of Quick Sort is O (n log n).

Enrollment No. 221420107010


Practical – 04
❖ Implementation of knapsack problem using geedy algorithm

Codes:

# include<stdio.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("\nThe result vector is:- ");

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

printf("%f\t", x[i]);

printf("\nMaximum profit is:- %f", tp);

} int main() {

float weight[20], profit[20], capacity;

int num, i, j;

float ratio[20], temp;

printf("\nEnter the no. of objects:- ");

scanf("%d", &num);

Enrollment No. 221420107010


printf("\nEnter the wts and profits of each object:- ");

for (i = 0; i < num; i++) {

scanf("%f %f", &weight[i], &profit[i]); }

printf("\nEnter the capacity of knapsack:- ");

scanf("%f", &capacity);

for (i = 0; i < num; i++) {

ratio[i] = profit[i] / weight[i];

} for (i = 0; i < num; i++) {

for (j = i + 1; j < num; 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(num, weight, profit, capacity);

return(0);

Output:

Enter the no. of objects:- 4


Enter the wts and profits of each object:- 12 13 12 14 15 32 35 54
Enter the capacity of knapsack:- 100
The result vector is:- 1.000000 1.000000 1.000000 1.000000
Maximum profit is:- 113.000000

Enrollment No. 221420107010


Practical – 05
❖ Implementation of knapsack problem using dynamic programming.
codes:
#include<stdio.h>
int v[20][20];
int max1(int a,int b)
{
return(a>b)?a:b;
}
int main()
{
int i,j,p[20],w[20],n,max;
printf("\n enter the number of items\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{

printf("\n enter the weight and profit of the item %d:",i);


scanf("%d %d",&w[i],&p[i]);
}
printf("\n enter the capacity of the knapsack");
scanf("%d",&max);
for(i=0;i<=n;i++)
v[i][0]=0;
for(j=0;j<=max;j++)
v[0][j]=0;
for(i=1;i<=n;i++)
for(j=1;j<=max;j++)
{
if(w[i]>j)
v[i][j]=v[i-1][j];
else
v[i][j]=max1(v[i-1][j],v[i-1][j-w[i]]+p[i]);

Enrollment No. 221420107010


}
printf("\n\nThe table is\n");
for(i=0;i<=n;i++){
for(j=0;j<=max;j++)
printf("%d\t",v[i][j]);
printf("\n");
}
printf("\nThe maximum profit is %d",v[n][max]);
printf("\nThe most valuable subset is:{");
j=max;
for(i=n;i>=1;i--)
if(v[i][j]!=v[i-1][j]){
printf("\t item %d:",i);

j=j-w[i]; }
printf("}");
return 0;
}
Output:
enter the number of items
3
enter the weight and profit of the item 1:2 4
enter the weight and profit of the item 2:3 5
enter the weight and profit of the item 3:4 3
enter the capacity of the knapsack 55
The table is
0 0 0 0 0 0
0 0 4 4 4 4
0 0 4 5 5 9
0 0 4 5 5 9

The maximum profit is 9


The most valuable subset is:{ item 2: item 1:}

Enrollment No. 221420107010


Practical – 06
❖ Implementation of chain matrixmultiplication using dynamic programming.
Codes:
#include<stdio.h>
#include<limits.h>
int MatrixChainMultiplication(int p[], int n)
{ int m[n][n];
int i, j, k, L, q;
for (i=1; i<n; i++)
m[i][i] = 0;
for (L=2; L<n; L++)
{ for (i=1; i<n-L+1; i++)
{
j = i+L-1;
m[i][j] = INT_MAX;
for (k=i; k<=j-1; k++)
{ q = m[i][k] + m[k+1][j] + p[i-1]*p[k]*p[j];
if (q < m[i][j])
{
m[i][j] = q;
} } } }
return m[1][n-1];
}
int main()
{
int n,i;
printf("Enter number of matrices\n");
scanf("%d",&n);
n++;
int arr[n];
printf("Enter dimensions \n");
for(i=0;i<n;i++) {
printf("Enter d%d :: ",i);
scanf("%d",&arr[i]); }
int size = sizeof(arr)/sizeof(arr[0]);

Enrollment No. 221420107010


printf("Minimum number of multiplications is %d ", MatrixChainMultiplication(arr, size));
return 0;
}
Output:
Enter number of matrices
4
Enter dimensions
Enter d0 :: 23
Enter d1 :: 65
Enter d2 :: 12
Enter d3 :: 89
Enter d4 :: 23
Minimum number of multiplications is 48852

Enrollment No. 221420107010


Practical – 11
❖ Implement ff LCS problem.
Code:
#include<stdio.h>
#include<limits.h
int MatrixChainMultiplication(int p[], int n)
{
int m[n][n];
int i, j, k, L, q;

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


m[i][i] = 0; //number of multiplications are 0(zero) when there is only one matrix

for (L=2; L<n; L++)


{
for (i=1; i<n-L+1; i++)
{
j = i+L-1;
m[i][j] = INT_MAX; //assigning to maximum value

for (k=i; k<=j-1; k++)


{
q = m[i][k] + m[k+1][j] + p[i-1]*p[k]*p[j];
if (q < m[i][j])
{
m[i][j] = q; //if number of multiplications found less that number will be updated.
}
}
}
}

return m[1][n-1];
}

Enrollment No. 221420107010


int main()
{
int n,i;
printf("Enter number of matrices\n");
scanf("%d",&n);

n++;
int arr[n];
printf("Enter dimensions \n");
for(i=0;i<n;i++)
{
printf("Enter d%d :: ",i);
scanf("%d",&arr[i]);
}

int size = sizeof(arr)/sizeof(arr[0]);

printf("Minimum number of multiplications is %d ", MatrixChainMultiplication(arr, size));

return 0;
}
Output:
Enter number of matrices
2
Enter dimensions
Enter d0 :: 12
Enter d1 :: 23
Enter d2 :: 24
Minimum number of multiplications is 6624

Enrollment No. 221420107010

You might also like