Ayush Ada
Ayush Ada
Ayush Ada
NAME: Ayush
Patel
EN.NO:20041010
7100
CLASS: TY-CE-2
BATCH:A
PRACTICAL LIST
Practical Practical
No. Name
Implementation and Time analysis of sorting algorithms.
1 Bubble sort, Selection sort, Insertion sort, Merge sort and
Quicksort
Implementation and Time analysis of linear and binary
2
search algorithm.
3 Implementation of max-heap sort algorithm
Implementation and Time analysis of factorial program
4
using iterative and recursive method
Implementation of a knapsack problem using
5
dynamic programming.
Implementation of chain matrix multiplication using dynamic
6
programming.
Implementation of making a change problem using dynamic
7
programming
8 Implementation of a knapsack problem using greedy algorithm
9 Implementation of Graph and Searching (DFS and BFS).
10 Implement prim’s algorithm
11 Implement Kruskal’s algorithm.
12 Implement LCS problem.
TY CE 2 AyushPatel 200410107100
Practical 1
Aim: Implementation and Time analysis of sorting algorithms.
Selection Sort :
PROGRAM CODE:
#include<stdio.h
> int main()
{
int i,j,count,temp,number[10];
printf("Enter the numbers of
elements:"); scanf("%d",&count);
printf("Enter %d elements:",count);
for(i=0;i<count;i++)
scanf("%d",&number[i])
;
for(i=0;i<count;i++)
{
for(j=i+1;j<count;j++)
{
If(number[i]>number[j]
){ temp=number[i];
number[i]=number[j];
number[j]=temp;
}
}
}
printf("Sorted Elements:");
TY CE 2 AyushPatel 200410107100
for(i=0;i<count;i++) printf("\t
%d",number[i]
);
return 0;
OUTPUT:
Insertion Sort :
PROGRAM CODE:
#include<stdio.h
> int main()
{
int i, j, n, k, a[25];
printf("Enter the No. of Elements :
"); scanf("%d",&n);
printf("Enter %d elements: ",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=1;i<n;i++)
{
TY CE 2 AyushPatel 200410107100
k=a[i];
j=i-1;
while((k<a[j])&&(j>=0))
{
a[j+1]=a[j]
; j=j-1;
}
a[j+1]=k;
}
return 0;
}
OUTPUT:
TY CE 2 AyushPatel 200410107100
PRATICAL 2
AIM: Implementation and Time analysis of linear and binary
search algorithm.
LINEAR
SEARCH: PROGRAM
CODE:
#include<stdio.h
>
#include<conio.h
int a[20],i,size,item,pos,flag=0;
"); scanf("%d",&size);
for(i=0;i<size;i++)
scanf("%d",&a[i]);
"); scanf("%d",&item);
for(i=0;i<size;i++)
TY CE 2 AyushPatel 200410107100
if(item==a[i])
pos=i;
flag=1;
break;
if(flag==1)
getch();
OUTPUT :
TY CE 2 AyushPatel 200410107100
BINARY
SEARCH: PROGRAM
CODE:
#include
<stdio.h> int
main()
{
int c, low, high, middle, n, search, array[100];
printf("Enter value to
find\n"); scanf("%d",
&search);
low = 0;
high = n - 1;
middle = (low+high)/2;
return 0;
}
OUTPUT:
TY CE 2 AyushPatel 200410107100
PRACTICAL 3
Aim: Implementation of max-heap sort algorithm
PROGRAM CODE:
#include<stdio.h
[]);
void down_adjust(int
int heap[30],n,i,last,temp;
printf("Enter no. of
elements:"); scanf("%d",&n);
printf("\nEnter elements:");
for(i=1;i<=n;i++)
scanf("%d",&heap[i])
; heap[0]=n;
create(heap);
heap[last] last=heap[0];
temp=heap[1];
heap[1]=heap[last]
;
TY CE 2 AyushPatel 200410107100
heap[last]=temp;
heap[0];
down_adjust(heap,1)
printf("\nArray after
sorting:\n"); for(i=1;i<=n;i++)
printf("%d ",heap[i]);
int i,n;
n=heap[0]; //no. of
elements for(i=n/2;i>=1;i--)
down_adjust(heap,i);
int j,temp,n,flag=1;
n=heap[0];
heap[j]) j=j+1;
if(heap[i] >
heap[j])
flag=0;
else
temp=heap[i];
heap[i]=heap[j];
heap[j]=temp;
i=j;
OUTPUT:
TY CE 2 AyushPatel 200410107100
PRACTICAL 4
AIM: Implementation and Time analysis of factorial program using iterative
and recursive method.
PROGRAM CODE:
#include<stdio.h
> int fact(int);
void main()
{
int n,f;
printf("Enter the
number");
scanf("%d",&n);
f=fact(n);
printf("factorial=%d",
f);
}
int fact(int n)
{
if(n==0)
return 1;
else
if(n==1)
return 1;
else
return n*fact(n-1);
}
OUTPUT:
TY CE 2 AyushPatel 200410107100
Factorial using
#include<stdio.h
> int main()
{
int i,fact=1,number;
printf("Enter the
number:");
scanf("%d",&number);
for(i=1;i<=number;i++)
{
fact=fact*i;
}
printf("Factorial of %d
is: %d",number,fact); return 0;
}
OUTPUT:
TY CE 2 AyushPatel 200410107100
PRACTICAL 5
AIM: Implementation of a knapsack problem using dynamic
#include<stdio.h>
int n)
int i, w;
int K[n+1][W+1];
if (i==0 || w==0)
K[i][w] = 0;
1][w]); else
K[i][w] = K[i-1][w];
}
TY CE 2 AyushPatel 200410107100
return K[n][W];
int main()
printf("Enter number of
printf("Enter size of
n)); return 0;
}
TY CE 2 AyushPatel 200410107100
OUTPUT:
TY CE 2 AyushPatel 200410107100
PRACTICAL 6
AIM: Implementation of chain matrix multiplication using dynamic
#include <stdio.h>
#include<limits.h>
#define INFY
999999999 long int
m[20][20];
int s[20][20];
int p[20],i,j,n;
void matmultiply(void)
{
long int q;
int k;
for(i=n;i>0;i--)
{
for(j=i;j<=n;j++)
{
if(i==j)
m[i][j]=0;
else
TY CE 2 AyushPatel 200410107100
{
for(k=i;k<j;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;
s[i][j]=k;
}
}
}
}
}
}
void main()
{
TY CE 2 AyushPatel 200410107100
int k;
printf("Enter the no. of elements:
"); scanf("%d",&n);
for(i=1;i<=n;i++)
for(j=i+1;j<=n;j+
+)
{
m[i][i]=0;
m[i][j]=INFY;
s[i][j]=0;
}
for(k=0;k<=n;k++)
{
printf("P%d: ",k);
scanf("%d",&p[k]);
}
matmultiply();
printf("\nCost Matrix
M:\n"); for(i=1;i<=n;i++)
for(j=i;j<=n;j++)
printf("m[%d][%d]: %ld\n",i,j,m[i][j]);
i=1,j=n;
printf("\nMultiplication Sequence :
"); print_optimal(i,j);
printf("\nMinimum number of multiplications
is : %d ", MatrixChainOrder(p, 1, n));
}
TY CE 2 AyushPatel 200410107100
OUTPUT:
TY CE 2 AyushPatel 200410107100
PRACTICAL 7
#include<stdio.h>
int count(int S[],int m,int n)
{
if(n==0)
return 1;
if(n<0)
return 0;
if(m<=0 &&
n>=1)
return 0;
return count(S,m-1,n)+count(S,m,n-S[m-1]);
}
int main()
{
int i,j;
int arr[]={4,1,2};
int m=sizeof(arr)/sizeof(arr[0]);
printf("The solution
is:""%d",count(arr,m,8)); getchar();
return 0;
}
OUTPUT:
TY CE 2 AyushPatel 200410107100
PRACTICAL 8
AIM: Implementation of a knapsack problem using greedy
#include
<stdio.h> void
main()
{
int capacity, no_items, cur_weight,
item; int used[10];
float total_profit;
int i;
int
weight[10];
int value[10];
printf("Enter the capacity of knapsack:\
n"); scanf("%d", &capacity);
printf("Enter the number of items:\
n"); scanf("%d", &no_items);
printf("Enter the weight and value of %d item:\n",
no_items); for (i = 0; i < no_items; i++)
{
printf("Weight[%d]:\t", i);
scanf("%d", &weight[i]);
printf("Value[%d]:\t", i);
scanf("%d", &value[i]);
}
for (i = 0; i < no_items;
++i) used[i] = 0;
cur_weight =
capacity; while
(cur_weight > 0)
{
item = -1;
for (i = 0; i < no_items; ++i)
TY CE 2 AyushPatel 200410107100
if ((used[i] == 0) &&
((item == -1) || ((float) value[i] / weight[i] > (float)
value[item] / weight[item])))
item = i;
used[item] = 1;
cur_weight -=
weight[item]; total_profit
+= value[item]; if
(cur_weight >= 0)
printf("Added object %d (%d Rs., %dKg) completely in the bag.
Space left: %d.\n", item + 1, value[item], weight[item],
cur_weight);
else
{
int item_percent = (int) ((1 + (float) cur_weight / weight[item]) *
100); printf("Added %d%% (%d Rs., %dKg) of object %d in the
bag.\n", item_percent, value[item], weight[item], item + 1);
total_profit -= value[item];
total_profit += (1 + (float)cur_weight / weight[item]) * value[item];
}
}
printf("Filled the bag with objects worth %.2f Rs.\n", total_profit);
}
OUTPUT:
TY CE 2 AyushPatel 200410107100
PRACTICAL 9:
#include<stdio.h>
int G[10][10],visited[10],n;
void main()
{
int i,j;
printf("Enter number of
vertices:"); scanf("%d",&n);
printf("\nEnter adjenceny matrix of the
graph:"); for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&G[i][j])
;
for(i=0;i<n;i+
+)
visited[i]=0;
DFS(0);
}
void DFS(int i)
{
int j; printf("\n
%d",i)
; visited[i]=1;
for(j=0;j<n;j++)
if(!visited[j]&&G[i][j]==
1) DFS(j);
}
TY CE 2 AyushPatel 200410107100
OUTPUT:
#include<stdio.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;
printf("\n Enter the number of
vertices:"); scanf("%d", &n);
OUTPUT:
TY CE 2 AyushPatel 200410107100
PRACTICAL 10
AIM: Implement prim’s algorithm
PROGRAM CODE:
#include<stdio.h
> int main()
{
int cost[10]
[10],visited[10]={0},i,j,n,no_e=1,min,a,b,min_cost=0;
printf("Enter number of nodes ");
scanf("%d",&n);
printf("Enter cost in form of 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]=1000;
}
}
visited[1]=1;
while(no_e<n)
{
min=1000;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(cost[i][j]<min)
{
if(visited[i]!=0)
{
min=cost[i][j]
; a=i;
b=j;
TY CE 2 AyushPatel 200410107100
}
}
}
}
if(visited[b]==0)
{
printf("\n%d to %d cost=%d",a,b,min);
min_cost=min_cost+min;
no_e++;
}
visited[b]=1;
cost[a][b]=cost[b][a]=1000;
}
printf("\nminimum weight is%d",min_cost);
return 0;
}
OUTPUT:
TY CE 2 AyushPatel 200410107100
PRACTICAL 11
AIM: Implement
Kruskal’s
}
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)
{
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:
TY CE 2 AyushPatel 200410107100
PRACTICAL 12:
AIM: Implement LCS
#include<stdio.h
>
#include<string.h
>
int i,j,m,n,c[20][20];
char x[20],y[20],b[20][20];
void lcs(
{
m=strlen(x);
n=strlen(y);
for(i=0;i<=m;i+
+)
c[i][0]=0;
for(i=0;i<=n;i++)
c[0][i]=0;
for(i=1;i<=m;i+
+)
for(j=1;j<=n;j++)
{
if(x[i-1]==y[j-1])
{
c[i][j]=c[i-1][j-1]+1;
TY CE 2 AyushPatel 200410107100
b[i][j]='c';
}
else if(c[i-1][j]>=c[i][j-1])
{
c[i][j]=c[i-
1][j];
} b[i][j]='u';
else
{
c[i][j]=c[i][j-
} 1]; b[i][j]='l';
}
}
int main()
{
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: