ADA Practicals
ADA Practicals
Practical:- 1
AIM : Implementation and Time analysis of sorting algorithms.Bubble sort, Selection
sort, Insertion sort, Merge sort and Quicksort.
Insertion sort :
Insertion sort is a simple sorting algorithm that works similar to the way you sort playing cards
in your hands.
Algorithm of insertion sort :
for j=2 to A.length
key = A[j]
i = j -1
while( A[i] > key && i > 0 )
A[ i + 1 ] = a[i]
i = i -1
A[ i + 1 } = key
C program of insertion sort :
#include <stdio.h>
void insertionSort(int arr[], int n) {
int i, key, j;
for (i = 1; i < n; i++) {
key = arr[i];
j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
void printArray(int arr[], int n) {
int i;
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}
int main()
{
int arr[] = {19, 20, 60, 25, 10};
int n = sizeof(arr) / sizeof(arr[0]);
insertionSort(arr, n);
printf("Sorted array: \n");
printArray(arr, n);
1
VIEAT/B.E./SEM-V/220943107040
ADA(3150703)
return 0;
}
Output:
Bubble sort :
In this algorithm, traverse from left and compare adjacent elements and the higher one is
placed at right side.
2
VIEAT/B.E./SEM-V/220943107040
ADA(3150703)
printf("\n");
}
int main()
{
int arr[] = {62, 43, 20, 23, 110, 10, 90};
int n = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr, n);
printf("Sorted array: \n");
printArray(arr, n);
return 0;
}
Output :
Selection sort :
Selection sort is an effective and efficient sort algorithm based on comparison
operations.
3
VIEAT/B.E./SEM-V/220943107040
ADA(3150703)
int main() {
int arr[] = {62, 90, 43, 23, 10};
int n = sizeof(arr) / sizeof(arr[0]);
selectionSort(arr, n);
printf("Sorted array: \n");
printArray(arr, n);
return 0;
}
Output :
Merge sort :
Merge sort continuously cuts down a list into multiple sublists until each has only one item,
then merges those sublists into a sorted list.
Algorithm for merge sort :
4
VIEAT/B.E./SEM-V/220943107040
ADA(3150703)
Output :
5
VIEAT/B.E./SEM-V/220943107040
ADA(3150703)
Quick sort :
Quicksort is a divide-and-conquer algorithm. It works by selecting a 'pivot' element from the
array and partitioning the other elements into two sub-arrays, according to whether they are less than
or greater than the pivot. For this reason, it is sometimes called partition-exchange sort.
Algorithm for quick sort :
6
VIEAT/B.E./SEM-V/220943107040
ADA(3150703)
Output :
7
VIEAT/B.E./SEM-V/220943107040
ADA(3150703)
Practical :- 2
AIM: Implementation and Time analysis of linear and binary search algorithm.
Algorithm:
Program:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define size 50
void main()
{
int e, a[500], i, c = 0;
clock_t end, start;
double cpu;
printf("Enter element to search: ");
scanf("%d", &e);
printf("\n\n");
for (i = 0; i < 50; i++)
{
a[i] = rand() % 100;
}
for (i = 0; i < 50; i++)
{
printf("%d ", a[i]);
}
printf("\n\nThe found elements are: \n");
start = clock();
for (i = 0; i < 50; i++)
{
if (a[i] == e)
{
printf("%d ", a[i]);
c++;
}
}
end = clock();
cpu = ((double)(end = start) / CLOCKS_PER_SEC);
printf("\n\nThe number of elements found are: %d ", c);
printf("\n\nThe time taken is: %lf\n\n", cpu);
}
8
VIEAT/B.E./SEM-V/220943107040
ADA(3150703)
Output :
9
VIEAT/B.E./SEM-V/220943107040
ADA(3150703)
Practical :- 3
AIM: Implementation of max-heap sort algorithm.
Algorithm:
Program:
#include <stdio.h>
#include <stdlib.h>
int size;
void max_heapify(int *, int);
void build_max(int *);
void heapsort(int *);
int heap_size;
void main()
{
printf("Enter size of array\n");
scanf("%d", &size);
int a[size], i, j, p, k = size - 1;
printf("Enter %d elements in array\n", size);
for (i = 0; i < size; i++)
scanf("%d", &a[i]);
heapsort(a);
printf("\n\n");
printf("Sorted heap is\n");
for (i = 0; i < size; i++)
printf("%d ", a[i]);
printf("\n\n");
int y = log2(size + 1) + 1, l = 0;
for (j = 0; j <= log2(size + 1) + 1; j++)
{
printf("\n");
for (p = y; p >= 0; p--)
printf(" ");
y--;
for (i = 0; (i < pow(2, j) && l < size); i++)
{
printf("%d ", a[k]);
k--;
l++;
10
VIEAT/B.E./SEM-V/220943107040
ADA(3150703)
}
printf("\n");
}
}
11
VIEAT/B.E./SEM-V/220943107040
ADA(3150703)
d[a] = d[largest];
d[largest] = p;
max_heapify(d, largest);
}
}
Output:
12
VIEAT/B.E./SEM-V/220943107040
ADA(3150703)
Practical :- 4
AIM: Implementation and Time analysis of factorial program using iterative and
recursive method.
Algorithm:
Program:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int fact(int x)
{
double f = x;
if (x != 1)
f *= fact(x - 1);
else
return 1;
return f;
}
void main()
{
int i, n;
double a, fa = 1;
clock_t end, start;
double cpu;
printf("\nEnter a number to find factorial ");
scanf("%d", &n);
start = clock();
13
VIEAT/B.E./SEM-V/220943107040
ADA(3150703)
Output:
14
VIEAT/B.E./SEM-V/220943107040
ADA(3150703)
Practical :- 5
AIM: Implementation of a knapsack problem using dynamic programming.
Algorithm:
Program:
#include <stdio.h>
int max
{
return (a > b)? a : b;
}
// Returns the maximum value that can be put in a knapsack of capacity W
int knapsack(int W, int wt[], int val[], int n)
{
int i, w;
int K[n+1][W+1];
return K[n][W];
}
int main()
{
int val[] = {60, 100, 120};
int wt[] = {10, 20, 30};
int W = 50;
int n = sizeof(val)/sizeof(val[0]);
printf("\nValue = %d", knapsack(W, wt, val, n));
return 0;
15
VIEAT/B.E./SEM-V/220943107040
ADA(3150703)
Output:
16
VIEAT/B.E./SEM-V/220943107040
ADA(3150703)
Practical :-:6
Program:
#include <stdio.h>
#include <stdlib.h>
void sel(int arr[5][5], int, int);
int n, i, j, s[10][10];
int main()
{
int l, k, q;
printf("Enter number of matrices\n");
scanf("%d", &n);
int p[n + 1], m[n + 1][n + 1];
printf("\nEnter P array values\n");
17
VIEAT/B.E./SEM-V/220943107040
ADA(3150703)
}
printf("\n\nm Table\n\n");
for (i = 1; i < n + 1; i++)
{
for (j = 1; j < n + 1; j++)
{
printf("%d\t", m[i][j]);
}
printf("\n");
}
printf("\n\n\ns Table\n\n");
18
VIEAT/B.E./SEM-V/220943107040
ADA(3150703)
Output:
19
VIEAT/B.E./SEM-V/220943107040
ADA(3150703)
Practical :- :7
Program:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int m, n, i, j;
printf("Enter the amount of which you want change\n");
scanf("%d", &m);
printf("\nEnter the number of determinants\n");
scanf("%d", &n);
int c[n + 1][m + 1], dc[n + 1];
printf("\nEnter the determinants\n");
dc[0] = 0;
for (i = 1; i < n + 1; i++)
scanf("%d", &dc[i]);
for (i = 0; i < n + 1; i++)
{
for (j = 0; j <= m; j++)
c[i][j] = 0;
}
for (i = 0; i < n + 1; i++)
c[i][0] = 0;
for (i = 0; i < m + 1; i++)
{
c[0][i] = i;
}
for (i = 1; i < n + 1; i++)
{
for (j = 1; j < m + 1; j++)
{
if (dc[1] > c[0][j])
c[i][j] = 0;
else if (dc[i] <= j)
{
if ((c[i][j - dc[i]] + 1) < c[i - 1][j])
c[i][j] = (c[i][j - dc[i]] + 1);
else
c[i][j] = c[i - 1][j];
}
else
c[i][j] = c[i - 1][j];
20
VIEAT/B.E./SEM-V/220943107040
ADA(3150703)
}
}
printf("\n");
for (i = 0; i < n + 1; i++)
{
printf("%d ", dc[i]);
for (j = 0; j < m + 1; j++)
{
printf(" %d", c[i][j]);
}
printf("\n");
}
printf("\nThe change you get is \n");
i = n;
j = m;
while (i != 1)
{
if (c[i][j] == c[i - 1][j])
i = i - 1;
else
{
j = j - dc[i];
printf(" %d ", dc[i]);
}
}
printf("\n\n");
return 0;
}
Output:
21
VIEAT/B.E./SEM-V/220943107040
ADA(3150703)
Practical :-:8
Program:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int m, n, i, j;
printf("Enter maximum weight of knapsack ");
scanf("%d", &m);
printf("\nEnter number of objects ");
scanf("%d", &n);
int wt = 0, k = 0;
float cal[n], p[n], w[n], x[n], prof = 0;
for (i = 0; i < n; i++)
x[i] = 0;
printf("\nEnter weights\n");
for (i = 0; i < n; i++)
{
printf("w[%d] = ", i);
scanf("%f", &w[i]);
}
printf("\nEnter profits\n");
for (i = 0; i < n; i++)
{
printf("p[%d] = ", i);
scanf("%f", &p[i]);
}
for (i = 0; i < n; i++)
cal[i] = p[i] / w[i];
for (i = 0; i < n; i++)
{
for (j = i + 1; j < n; j++)
{
if (cal[i] < cal[j])
{
int t1, t2, t3;
t1 = cal[i];
cal[i] = cal[j];
cal[j] = t1;
t2 = w[i];
w[i] = w[j];
w[j] = t2;
t3 = p[i];
p[i] = p[j];
p[j] = t3;
22
VIEAT/B.E./SEM-V/220943107040
ADA(3150703)
}
}
}
printf("\n\n p[i]\t\t w[i]\t\t cal[i]\n");
for (i = 0; i < n; i++)
printf("%f\t %f\t %f\t\n", p[i], w[i], cal[i]);
for (i = 0; i < n; i++)
{
if ((wt + w[i]) <= m)
{
k++;
x[i] = 1;
wt += w[i];
prof += p[i];
}
else
{
k++;
x[i] = (m - wt) / w[i];
w[i] = m - wt;
wt = m;
prof += (x[i] * p[i]);
p[i] = (x[i] * p[i]);
break;
}
}
printf("\nThe selected weights are \n\ni\t w[i]\t\t p[i]\n");
for (i = 0; i < k; i++)
printf("%d\t%f\t%f\n", i + 1, w[i], p[i]);
printf("\n\nThe total profit is %f\n\n", prof);
return 0;
}
23
VIEAT/B.E./SEM-V/220943107040
ADA(3150703)
Output:
24
VIEAT/B.E./SEM-V/220943107040
ADA(3150703)
Practical :- : 9
Program :
#include<stdio.h>
#include<stdlib.h>
for(i=0;i<n;i++)
visited[i]=0;
DFS(0);
}
void DFS(int i)
{
node *p;
printf("\n%d",i);
p=G[i];
visited[i]=1;
while(p!=NULL)
{
i=p->vertex;
if(!visited[i])
DFS(i);
p=p->next;
25
VIEAT/B.E./SEM-V/220943107040
ADA(3150703)
}
}
void read_graph()
{
int i,vi,vj,no_of_edges;
printf("Enter number of vertices:");
scanf("%d",&n);
//initialise G[] with a null
for(i=0;i<n;i++)
{
G[i]=NULL;
//read edges and insert them in G[]
while(p->next!=NULL)
p=p->next;
p->next=q;
}
}
26
VIEAT/B.E./SEM-V/220943107040
ADA(3150703)
Output :
BFS :
#include<stdio.h>
int main()
{
// defining the number of total persons
int N = 6;
27
VIEAT/B.E./SEM-V/220943107040
ADA(3150703)
for(int i=0;i<6;i++)
{
// adding non-visited connected nodes of the current node to the queue
if((graph[current-1][i] == 1) && (visited[i] == 0))
{
visited[i] = 1; // marking visisted
push(i+1);
}
}
}
return 0;
}
OUTPUT:
28
VIEAT/B.E./SEM-V/220943107040
ADA(3150703)
PRACTICAL :-10
AIM: Implement prim’s algorithm
PROGRAM:
#include <iostream>
using namespace std;
int main()
{
int n;
char c = 'a';
cout << "enter number of nodes";
cin >> n;
int g[n][n];
int stree[n][3];
for (int i = 0; i < n; i++)
stree[i][0] = stree[i][1] = stree[i][2] = -1;
cout << "enter graph egdes 0 for no egde and weight for edge\n";
for (int i = 0; i < n; i++)
{
for (int j = i; j < n; j++)
{
g[i][j] = 0;
if (i != j)
{
cout << "(" << ((char)(c + i)) << "," << (char)(c + j) << "):-";
cin >> g[i][j];
g[j][i] = g[i][j];
}
}
}
cout << "\t ";
for (int i = 0; i < n; i++)
cout << (char)(c + i) << "\t";
for (int i = 0; i < n; i++)
{
cout << endl
<< (char)(c + i) << "\t ";
for (int j = 0; j < n; j++)
{
if (g[i][j] != 0)
cout << "|" << g[i][j];
else
cout << "|-";
cout << "\t";
}
}
// root node
stree[0][0] = stree[0][1] = 0;
stree[0][2] = 1;
29
VIEAT/B.E./SEM-V/220943107040
ADA(3150703)
// making stree
int counter = 0, i = 0;
while (counter < n)
{
for (int k = 0; k < n; k++)
{
if (g[i][k] != 0)
{
if (stree[k][0] == -1 && stree[k][2] != 1)
{
stree[k][0] = i;
stree[k][1] = g[i][k];
}
else
{
if (stree[k][1] > g[i][k] && stree[k][2] != 1)
{
stree[k][0] = i;
stree[k][1] = g[i][k];
}
}
}
}
int temp = -1;
for (int k = 0; k < n; k++)
{
if (stree[k][2] != 1 && stree[k][0] != -1)
{
if (temp == -1)
temp = k;
else
{
if (stree[temp][1] > stree[k][1])
temp = k;
}
}
}
i = temp;
stree[i][2] = 1;
counter++;
}
for (i = 0; i < n; i++)
cout << endl
<< (char)(c + i) << ":-" << (char)(c + stree[i][0]) << "," << stree[i][1];
}
30
VIEAT/B.E./SEM-V/220943107040
ADA(3150703)
OUTPUT:
31
VIEAT/B.E./SEM-V/220943107040
ADA(3150703)
PRACTICAL :-:11
AIM: Implement Kruskal’s Algorithm
PROGRAM:
#include <iostream>
using namespace std;
class edge
{
public:
int w = 0, e1 = 0, e2 = 0;
};
int main()
{
int n;
char c = 'a';
int k = 0;
cout << "enter number of nodes";
cin >> n;
int g[n][n];
int stree[n][n], snode[n];
edge e[n * n];
// defalut values for tree
for (int i = 0; i < n; i++)
stree[i][0] = stree[i][1] = stree[i][2] = -1;
cout << "enter graph egdes 0 for no egde and weight for edge\n";
for (int i = 0; i < n; i++)
{
for (int j = i; j < n; j++)
{
g[i][j] = 0;
if (i != j)
{
cout << "(" << ((char)(c + i)) << "," << (char)(c + j) << "):-";
cin >> g[i][j];
g[j][i] = g[i][j];
if (g[i][j] != 0)
{
e[k].w = g[i][j];
e[k].e1 = i;
e[k++].e2 = j;
}
}
}
}
// printing graph table
cout << "\t ";
32
VIEAT/B.E./SEM-V/220943107040
ADA(3150703)
for (int i = 0; i < n; i++)
cout << (char)(c + i) << "\t";
for (int i = 0; i < n; i++)
{
cout << endl
<< (char)(c + i) << "\t ";
for (int j = 0; j < n; j++)
{
if (g[i][j] != 0)
cout << "|" << g[i][j];
else
cout << "|-";
cout << "\t";
}
}
// sorting
for (int i = 0; i < k; i++)
{
for (int j = k - 1; j > i; j--)
{
if (e[i].w > e[j].w)
{
edge tem;
tem = e[i];
e[i] = e[j];
e[j] = tem;
}
}
}
// printing sorted edges
for (int i = 0; i < k; i++)
cout << endl
<< e[i].w << "(" << (char)(c + e[i].e1) << "," << (char)(c + e[i].e2) << ")";
// make graph-tree table;
for (int i = 0; i < n; i++)
{
snode[i] = -1;
for (int j = 0; j < n; j++)
stree[i][j] = -1;
}
int gp = 0;
for (int i = 0; i < k; i++)
{
if ((snode[e[i].e1] == -1 && snode[e[i].e2] == -1))
{
stree[e[i].e1][e[i].e2] = stree[e[i].e2][e[i].e1] = e[i].w;
snode[e[i].e1] = snode[e[i].e2] = gp;
gp++;
}
33
VIEAT/B.E./SEM-V/220943107040
ADA(3150703)
else if (stree[e[i].e1][e[i].e2] == -1 && (snode[e[i].e1] != snode[e[i].e2]))
{
stree[e[i].e1][e[i].e2] = stree[e[i].e2][e[i].e1] = e[i].w;
if (snode[e[i].e1] != -1 && snode[e[i].e2] == -1)
{
snode[e[i].e2] = snode[e[i].e1];
}
else if (snode[e[i].e2] != -1 && snode[e[i].e1] == -1)
{
snode[e[i].e1] = snode[e[i].e2];
}
else
{
int temp = snode[e[i].e2];
for (int l = 0; l < n; l++)
{
if (snode[l] == temp)
{
snode[l] = snode[e[i].e1];
}
}
}
}
}
cout << endl
<< "\t ";
for (int i = 0; i < n; i++)
cout << (char)(c + i) << "\t ";
for (int i = 0; i < n; i++)
{
cout << endl
<< (char)(c + i) << "\t";
for (int j = 0; j < n; j++)
{
if (stree[i][j] != -1)
cout << "|" << stree[i][j];
else
cout << "|-";
cout << "\t";
}
}
}
34
VIEAT/B.E./SEM-V/220943107040
ADA(3150703)
OUTPUT:
35
VIEAT/B.E./SEM-V/220943107040
ADA(3150703)
PRACTICAL :-: 12
AIM: Implement LCS problem.
PROGRAM:
#include <stdio.h>
void lcs(int arr[][10], char ar[], int, int);
int l, k;
void main()
{
char s1[10], s2[10];
int i, j;
printf("\nEnter the strings\nX = ");
s1[0] = 0;
char ch = getchar();
while (ch != '\n')
{
s1[++l] = ch;
ch = getchar();
}
printf("Y = ");
s2[0] = 0;
char c1 = getchar();
while (c1 != '\n')
{
s2[++k] = c1;
c1 = getchar();
}
36
VIEAT/B.E./SEM-V/220943107040
ADA(3150703)
c[i][j] = c[i - 1][j - 1] + 1;
b[i][j] = 1; // diagonal
}
else if (c[i - 1][j] >= c[i][j - 1])
{
c[i][j] = c[i - 1][j];
b[i][j] = 2; // up
}
else
{
c[i][j] = c[i][j - 1];
b[i][j] = 3; // left
}
}
}
printf("\n\n\t");
for (i = 1; i < k + 1; i++)
{
printf("\t %c", s2[i]);
}
printf("\n");
for (i = 0; i < l + 1; i++)
{
printf("%c\t", s1[i]);
for (j = 0; j < k + 1; j++)
{
printf("%d(%d)\t", c[i][j], b[i][j]);
}
printf("\n");
}
printf("\n\n");
printf("Longest common substring of X and Y is ");
lcs(b, s1, l, k);
printf("\n\n");
}
void lcs(int arr[l + 1][k + 1], char ar[l + 1], int a, int p)
{
if (a == 0 || p == 0)
return;
if (arr[a][p] == 1)
{
lcs(arr, ar, a - 1, p - 1);
printf("%c ", ar[a]);
}
else if (arr[a][p] == 2)
{
lcs(arr, ar, a - 1, p);
}
else if (arr[a][p] == 3)
37
VIEAT/B.E./SEM-V/220943107040
ADA(3150703)
{
lcs(arr, ar, a, p - 1);
}
}
OUTPUT:
38
VIEAT/B.E./SEM-V/220943107040