ADA Hiren
ADA Hiren
ADA Hiren
(DEGREE)
SUBMITTED TO:-
COMPUTER DEPARTMENT
SCET, SAIJ, KALOL
SWAMINARAYAN COLLEGE OF ENNG. & TECH.
(DEGREE)
CERTIFICATE
Sr. Page
PRACTICAL AIM Date Sign
No No.
Implementation and Time analysis of sorting
1 algorithms.
1
Bubble sort, Selection sort, Insertion sort, Merge
sort and Quick sort
Implementation and Time analysis of linear and
2 binary search algorithm. 4
FACULTY: H.O.D:
5TH SEM- C.E (ADA) Enrollement No: 201150107012
PRACTICAL 1
#include<stdio.h>
#include<conio.h>
void main() {
int arr[30], num, i;
bubble_sort(arr, num);
getch();
}
1|Page
5TH SEM- C.E (ADA) Enrollement No: 201150107012
printf("\nUnsorted Data:");
for (k = 0; k < num; k++) {
printf("%5d", iarr[k]);
}
output:
2|Page
5TH SEM- C.E (ADA) Enrollement No: 201150107012
Enter no of elements :5
Enter array elements :10 4 55 21 6
Unsorted Data: 10 4 55 21 6
After pass 1 : 4 10 21 6 55
After pass 2 : 4 10 6 21 55
After pass 3 : 4 6 10 21 55
After pass 4 : 4 6 10 21 55
3|Page
5TH SEM- C.E (ADA) Enrollement No: 201150107012
PRACTICAL 2
AIM: Implementation and Time analysis of linear and binary search algorithm.
#include <stdio.h>
4|Page
5TH SEM- C.E (ADA) Enrollement No: 201150107012
int main()
{
//declaring variables
int array[1000],n,i;
5|Page
5TH SEM- C.E (ADA) Enrollement No: 201150107012
}
}
output:
6|Page
5TH SEM- C.E (ADA) Enrollement No: 201150107012
PRACTICAL 3
#include<stdio.h>
int main() {
int i, j, num, temp, arr[20];
7|Page
5TH SEM- C.E (ADA) Enrollement No: 201150107012
return 0;
}
Output :
Enter total elements: 5 Enter 5 elements: 9 4 1 0 2 After sorting: 0 1 2 4 9
1
2
3
8|Page
5TH SEM- C.E (ADA) Enrollement No: 201150107012
PRACTICAL 4
AIM: Implementation and Time analysis of factorial program using iterative and recursive
method.
#include <stdio.h>
#include <conio.h>
void main( )
{
int a[5] = { 11, 2, 9, 13, 57 } ;
int b[5] = { 25, 17, 1, 90, 3 } ;
int c[10] ;
int i, j, k, temp ;
clrscr( ) ;
9|Page
5TH SEM- C.E (ADA) Enrollement No: 201150107012
{
for ( j = i + 1 ; j <= 4 ; j++ )
{
if ( a[i] > a[j] )
{
temp = a[i] ;
a[i] = a[j] ;
a[j] = temp ;
}
if ( b[i] > b[j] )
{
temp = b[i] ;
b[i] = b[j] ;
b[j] = temp ;
}
}
}
for ( i = j = k = 0 ; i <= 9 ; )
{
if ( a[j] <= b[k] )
c[i++] = a[j++] ;
else
c[i++] = b[k++] ;
if ( j == 5 || k == 5 )
break ;
10 | P a g e
5TH SEM- C.E (ADA) Enrollement No: 201150107012
for ( ; j <= 4 ; )
c[i++] = a[j++] ;
for ( ; k <= 4 ; )
c[i++] = b[k++] ;
getch( ) ;
}
11 | P a g e
5TH SEM- C.E (ADA) Enrollement No: 201150107012
PRACTICAL 5
# include<stdio.h>
if (i < n)
x[i] = u / weight[i];
tp = tp + (x[i] * profit[i]);
12 | P a g e
5TH SEM- C.E (ADA) Enrollement No: 201150107012
int main() {
float weight[20], profit[20], capacity;
int num, i, j;
float ratio[20], temp;
13 | P a g e
5TH SEM- C.E (ADA) Enrollement No: 201150107012
temp = weight[j];
weight[j] = weight[i];
weight[i] = temp;
temp = profit[j];
profit[j] = profit[i];
profit[i] = temp;
}
}
}
Output :
Enter the no. of objects:- 7 Enter the wts and profits of each object:- 2 10 3 5 5 15 7 7 1 6 4 18 1
3 Enter the capacity of knapsack:- 15 The result vector is:- 1.000000 1.000000 1.000000
1.000000 1.000000 0.666667 0.000000 Maximum profit is:- 55.333332
14 | P a g e
5TH SEM- C.E (ADA) Enrollement No: 201150107012
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
15 | P a g e
5TH SEM- C.E (ADA) Enrollement No: 201150107012
13
16 | P a g e
5TH SEM- C.E (ADA) Enrollement No: 201150107012
PRACTICAL 6
#include <stdio.h>
int main()
{
int m, n, p, q, c, d, k, sum = 0;
int first[10][10], second[10][10], multiply[10][10];
if (n != p)
printf("Matrices with entered orders can't be multiplied with each other.\n");
else
{
printf("Enter the elements of second matrix\n");
17 | P a g e
5TH SEM- C.E (ADA) Enrollement No: 201150107012
multiply[c][d] = sum;
sum = 0;
}
}
printf("\n");
}
}
return 0;
18 | P a g e
5TH SEM- C.E (ADA) Enrollement No: 201150107012
output:
19 | P a g e
5TH SEM- C.E (ADA) Enrollement No: 201150107012
PRACTICAL 7
#include <stdio.h>
C = (int*)malloc((sum+1)*sizeof(int));
C[0] = 0;
20 | P a g e
5TH SEM- C.E (ADA) Enrollement No: 201150107012
}
C[i] = min;
}
int main()
{
int coin[] = {1,2,5};
coinchange(coin, 3 ,9);
}
21 | P a g e
5TH SEM- C.E (ADA) Enrollement No: 201150107012
PRACTICAL 8
#include<stdio.h>
if (i < n)
x[i] = u / weight[i];
22 | P a g e
5TH SEM- C.E (ADA) Enrollement No: 201150107012
tp = tp + (x[i] * profit[i]);
int main() {
float weight[20], profit[20], capacity;
int num, i, j;
float ratio[20], temp;
23 | P a g e
5TH SEM- C.E (ADA) Enrollement No: 201150107012
temp = weight[j];
weight[j] = weight[i];
weight[i] = temp;
temp = profit[j];
profit[j] = profit[i];
profit[i] = temp;
}
}
}
24 | P a g e
5TH SEM- C.E (ADA) Enrollement No: 201150107012
OUTPUT:
view source
Enter the no. of objects:- 7
25 | P a g e
5TH SEM- C.E (ADA) Enrollement No: 201150107012
PRACTICAL 9
#include<stdio.h>
void DFS(int);
int G[10][10],visited[10],n; //n is no of vertices and graph is sorted in array G[10][10]
void main()
{
int i,j;
printf("Enter number of vertices:");
scanf("%d",&n);
//read the adjecency matrix
printf("\nEnter adjecency matrix of the graph:");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&G[i][j]);
//visited is initialized to zero
for(i=0;i<n;i++)
visited[i]=0;
DFS(0);
}
void DFS(int i)
{
int j;
26 | P a g e
5TH SEM- C.E (ADA) Enrollement No: 201150107012
printf("\n%d",i);
visited[i]=1;
for(j=0;j<n;j++)
if(!visited[j]&&G[i][j]==1)
DFS(j);
}
27 | P a g e
5TH SEM- C.E (ADA) Enrollement No: 201150107012
PRACTICAL:9 BFS
include<stdio.h>
#include<conio.h>
int a[20][20],q[20],visited[20],n,i,j,f=0,r=-1;
void bfs(int v) {
for (i=1;i<=n;i++)
if(a[v][i] && !visited[i])
q[++r]=i;
if(f<=r) {
visited[q[f]]=1;
bfs(q[f++]);
}
}
void main() {
int v;
clrscr();
printf("\n Enter the number of vertices:");
scanf("%d",&n);
for (i=1;i<=n;i++) {
q[i]=0;
visited[i]=0;
}
printf("\n Enter graph data in matrix form:\n");
for (i=1;i<=n;i++)
for (j=1;j<=n;j++)
scanf("%d",&a[i][j]);
28 | P a g e
5TH SEM- C.E (ADA) Enrollement No: 201150107012
29 | P a g e
5TH SEM- C.E (ADA) Enrollement No: 201150107012
PRACTICAL 10
#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();
scanf("%d",&n);
for(i=1;i<=n;i++)
30 | P a g e
5TH SEM- C.E (ADA) Enrollement No: 201150107012
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)
31 | P a g e
5TH SEM- C.E (ADA) Enrollement No: 201150107012
min=cost[i][j];
a=u=i;
b=v=j;
if(visited[u]==0 || visited[v]==0)
mincost+=min;
visited[b]=1;
cost[a][b]=cost[b][a]=999;
32 | P a g e
5TH SEM- C.E (ADA) Enrollement No: 201150107012
getch();
output:
33 | P a g e
5TH SEM- C.E (ADA) Enrollement No: 201150107012
PRACTICAL 11
#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\tImplementation of Kruskal's algorithm\n");
printf("\nEnter the no. of vertices:");
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("The edges of Minimum Cost Spanning Tree are\n");
34 | P a g e
5TH SEM- C.E (ADA) Enrollement No: 201150107012
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("%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)
35 | P a g e
5TH SEM- C.E (ADA) Enrollement No: 201150107012
{
while(parent[i])
i=parent[i];
return i;
}
int uni(int i,int j)
{
if(i!=j)
{
parent[j]=i;
return 1;
}
return 0;
}
output:
36 | P a g e
5TH SEM- C.E (ADA) Enrollement No: 201150107012
PRACTICAL 12
int i,j,m,n,c[20][20];
char x[20],y[20],b[20][20];
void lcs()
{
m=strlen(x);
n=strlen(y);
37 | P a g e
5TH SEM- C.E (ADA) Enrollement No: 201150107012
for(i=0;i<=m;i++)
c[i][0]=0;
for(i=0;i<=n;i++)
c[0][i]=0;
38 | P a g e
5TH SEM- C.E (ADA) Enrollement No: 201150107012
{
printf("Enter 1st sequence:");
scanf("%s",x);
printf("Enter 2nd sequence:");
scanf("%s",y);
printf("\nThe Longest Common Subsequence is ");
lcs();
print(m,n);
return 0;
}
output:
39 | P a g e