ADA Practical File: Kartik Kataria
ADA Practical File: Kartik Kataria
ADA Practical File: Kartik Kataria
Practical File
KartikKataria
40525602715
5
Semester
Index
ProgramTitle
Date
Signature
1 SelectionSort 21-08-17
2 InsertionSort 21-08-17
3 QuickSort 21-08-17
4 MergeSort
5 LinearSort
6 BinarySort
7 MatrixMultiplication
8 LongestChainSubstring
9 OptimalBinaryTree
10 HuffmanCoding
11 DijkstraAlgorithm
12 Bellman-FordAlgorithm
1
Program 1
Selection Sort
#include<stdio.h>
#include<conio.h>
voidmain()
{
intarray[30],n,i,j,swap;
clrscr();
printf("\nEnternumberofelements:");
scanf("%d",&n);
printf("\nEnter%dintegers:",n);
for(i=0;i<n;i++)
scanf("%d",&array[i]);
for(i=0;i<n;i++){
for(j=i+1;j<n;j++){
if(array[i]>array[j]){
swap=array[i];
array[i]=array[j];
array[j]=swap;
}
}
}
printf("\nSortedlistinascendingorder:");
for(i=0;i<n;i++){
printf("%d",array[i]);
}
getch();
}
2
Program 1
Output
EnterNumberOfelements:
5
Enter5Integers:
3
2
1
-5
10
Sortedlistinascendingorder:
-5
1
2
3
5
10
3
Program 2
ort
Insertion S
#include<conio.h>
#include<stdio.h>
voidmain()
{
intn,array[30],c,d,temp;
clrscr();
printf("\nEnternumberofelements:");
scanf("%d",&n);
printf("\nEnter%dintegers:",n);
for(c=0;c<n;c++){
scanf("%d",&array[c]);
}
for(c=1;c<n;c++){
temp=array[c];
d=c-1;
while(d>=0&&temp<array[d]){
array[d+1]=array[d];
d=d-1;
}
array[d+1]=temp;
}
printf("\nSortedlistinascendingorder:");
for(c=0;c<n;c++){
printf("%d",array[c]);
}
getch();
}
4
Program 2
Output
Enternumberofelements:
6
Enter6Integers:
1
5
8
10
-3
2
Sortedlistinascendingorder
:
-3
1
2
5
8
10
5
Program 3
Merge Sort
#include<stdio.h>
#include<conio.h>
voidmergesort(int[],int,int);
voidmerge(int[],int,int,int, int);
voidmain()
{
intarray[30],i,n;
clrscr();
printf("\nEnternoofelements:");
scanf("%d",&n);
printf("\nEnter%delements:",n);
for(i=0;i<n;i++)
scanf("%d",&array[i]);
mergesort(array,0,n-1);
printf("\nSortedListinassendingorder:");
for(i=0;i<n;i++)
printf("%d",array[i]);
getch();
}
voidmerge(inta[],inti1,intj1,inti2,intj2)
{
inttemp[50],i,j,k;
i=i1;
j=i2;
k=0;
while(i<=j1&&j<=j2){
if(a[i]<a[j])
temp[k++]=a[i++];
else
temp[k++]=a[j++];
}
while(i<=j1)
temp[k++]=a[i++];
6
while(j<=j2)
temp[k++]=a[j++];
for(i=i1,j=0;i<=j2;i++,j++)
a[i]=temp[j];
}
voidmergesort(inta[],inti,intj)
{
intmid;
if(i<j){
mid=(i+j)/2;
mergesort(a,i,mid);
mergesort(a,mid+1,j);
merge(a,i,mid,mid+1,j);
}
}
7
Program 3
Output
EnterthelengthofArray:
5
Enter5Integers:
213
43
1
567
23
SortedListinascendingorder:
1
23
43
213
567
TimeComplexity
BestCase:n*log(n)
AverageCase:n*log(n)
Program 4
earch
Linear S
#include<stdio.h>
#include<conio.h>
intmain()
{
intarray[20],n,i,search;
clrscr();
printf("\nEnternumberof elements:");
scanf("%d",&n);
printf("\nEnter%dintegers:",n);
for(i=0;i<n;i++)
scanf("%d",&array[i]);
printf("\nEnterElementtoSearch:");
scanf("%d",&search);
for(i=0;i<n;i++){
if(array[i]==search){
printf("\nElementFoundat%dposition",i+1);
return0;
}
}
printf("\nElementNotFound");
return1;
}
Program 4
Output
Enternumberofelements:
6
Enter6Integers:
1
2
3
5
10
15
EnterElementtoSearch:
10
Elementfoundat5Position.
TimeComplexity
LinearSearch:O(n)
10
Program 5
Binary Search
#include<stdio.h>
#include<conio.h>
intmain()
{
inti,first,last,middle,n,search,array[30];
printf("\nEnternumberof elements:");
scanf("%d",&n);
printf("\nEnter%dintegers:",n);
for(i=0;i<n;i++)
scanf("%d",&array[i]);
printf("\nEnterElementtoSearch:");
scanf("%d",&search);
first=0;
last=n-1;
middle=(first+last)/2;
while(first<=last){
if(array[middle]< search)
first=middle+1;
elseif(array[middle]==search){
printf("%dfoundatlocation%d.\n",search,middle+1);
break;
}
else
last=middle-1;
middle=(first+last)/2;
}
if(first>last)
printf("Notfound!%disnotpresentinthelist.\n",search);
return0;
}
11
Program 5
Output
Enternumberofelements:
6
Enter6Integers:
1
5
2
4
6
8
EnterElementtoSearch:
4
4Foundat3Position.
TimeComplexity
BinarySearch:O(n*log(n))
12
Program 6
Matrix Multiplication
#include<stdio.h>
#include<conio.h>
intmata[5][5],matb[5][5],matc[5][5];
voidmain()
{
intr1,c1,r2,c2,p,x,y;
clrscr();
printf("\nEnterRows&ColumnofMatrixA:");
scanf("%d%d",&r1,&c1);
printf("\nEnterRows&ColumnofMatrixB:");
scanf("%d%d",&r2,&c2);
if(c2==r1){
printf("\nEnterMatrixA:");
for(x=0;x<r1;
x++)
for(y=0;y<c1;y++)
scanf("%d",&mata[x][y]);
printf("\nEnterMatrixB:");
for(x=0;x<r2;
x++)
for(y=0;y<c2;y++)
scanf("%d",&matb[x][y]);
for(x=0;x<r1;
x++)
for(y=0;y<c2;y++)
for(p=0;p<c1;p++)
matc[x][y]+=mata[x][p]*matb[p][y];
printf("\nProductofMatrixAandBis\n");
for(x=0;x<r1;
x++){
for(y=0;y<c2;y++)
printf("%2d",matc[x][y]);
printf("\n");
}
}
else
printf("\nMatrixMultiplicationNotPossible.c2!=r1");
}
13
Program 6
Output
EnterRows&ColumnsofMatrix
A:
2
2
EnterRows&ColumnsofMatrix
B:
2
2
EnterMatrixA:
2
3
45
56
EnterMatrixB:
21
32
34
54
TheProductis
144
226
2849
4464
TimeComplexity
O(n)
14
Program 7
LCS
#include<stdio.h>
#include<string.h>
#include<conio.h>
inti,j,
m,n,c[20][20];
charx[20],y[20],b[20][20];
voidprint(inti,intj)
{
if(i==0||j==0)
return;
if(b[i][j]=='c'){
print(i-1,j-1);
printf("%c",x[i-1]);
}
elseif(b[i][j]=='u')
print(i-1,j);
else
print(i,j-1);
}
voidlcs()
{
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;
//c,uandldenotescross,upwardanddownwarddirectionsrespectively
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;
b[i][j]='c';
}
elseif(c[i-1][j]>=c[i][j-1]){
c[i][j]=c[i-1][j];
15
b[i][j]='u';
}
else{
c[i][j]=c[i][j-1];
b[i][j]='l';
}
}
}
voidmain()
{
clrscr();
printf("Enter1stsequence:");
scanf("%s",x);
printf("Enter2ndsequence:");
scanf("%s",y);
printf("\nTheLongestCommonSubsequenceis");
lcs();
print(m,n);
getch();
}
16
Program 7
Output
Enter1stSequence:
AGGTAB
Enter2ndSequence:
GXTXAYB
TheLongestCommonSubsequence
is:
GTAB
TimeComplexity
WorstCase:O(2^(m+n))
17
Program 8
Dijkstras Algorithm
#include<stdio.h>
#include<conio.h>
#defineinfinity999
voiddij(intn,intv,intcost[10][10],intdist[])
{
inti,u,count,w,flag[10],min;
for(i=1;i<=n;i++)
flag[i]=0,dist[i]=cost[v][i];
count=2;
while(count<=n){
min=99;
for(w=1;w<=n;
w++)
if(dist[w]<min&&!flag[w])
min=dist[w],u=w;
flag[u]=1;
count++;
for(w=1;w<=n;
w++)
if((dist[u]+cost[u][w]<dist[w])&&!flag[w])
dist[w]=dist[u]+cost[u][w];
}
}
voidmain()
{
intn,v,i,j,cost[10][10],dist[10];
clrscr();
printf("nEnterthenumberofnodes:");
scanf("%d",&n);
printf("nEnterthecostmatrix: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]=infinity;
}
18
printf("nEnterthesourcematrix:");
scanf("%d",&v);
dij(n,v,cost,dist);
printf("nShortestpath:n");
for(i=1;i<=n;i++)
if(i!=v)
printf("%d->%d,cost=%dn",v,i,dist[i]);
getch();
}
19
Program 8
Output
Enterthenumberofnodes:
4
Enterthecostofweights
0
3
999
7
3
0
4
2
999
4
0
5
7
2
5
0
EnterSourceVertex:
1
TheShortestPathfromSource
1
to
all
nodes:
Source:1Destination:2Min
Cost:
3
Source:1Destination:3Min
Cost:
1
Source:1Destination:4Min
Cost:
5
TimeComplexity
O(V^2);O(E*log(V))
20
Program 9
Bellman-Ford
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<limits.h>
structEdge
{
//Thisstructureisequaltoanedge.Edgecontainstwoendpoints.Theseedges
aredirectededgessothey
//containsourceanddestinationandsomeweight.These3areelementsinthis
structure
intsource,destination,weight;
};
//astructuretorepresentaconnected,directedandweightedgraph
structGraph
{
intV,E;
//VisnumberofverticesandEisnumberofedges
structEdge*edge;
//Thisstructurecontainanotherstructurewhichwealreadycreatededge.
};
structGraph*createGraph(intV, intE)
{
structGraph*graph=(structGraph*)malloc(sizeof(structGraph));
//Allocatingspacetostructuregraph
graph->V=V;//assigningvaluestostructureelementsthattakenformuser.
graph->E=E;
graph->edge=(structEdge*) malloc(graph->E*sizeof(structEdge));
//Creating"Edge"typestructuresinside"Graph"structure,thenumberofedge
typestructuresareequaltonumberofedges
21
returngraph;
}
voidFinalSolution(intdist[],intn)
{
//Thisfunctionprintsthefinalsolution
printf("\nVertex\tDistancefromSourceVertex\n");
inti;
for(i=0;i<n;++i){
printf("%d\t\t%d\n",i,dist[i]);
}
}
voidBellmanFord(structGraph*graph,intsource)
{
intV=graph->V;
intE=graph->E;
intStoreDistance[V];
inti,j;
//Thisisinitialstepthat weknow,weinitializealldistancetoinfinity
exceptsource.
//Weassignsourcedistance as0(zero)
for(i=0;i<V;i++)
StoreDistance[i]=INT_MAX;
StoreDistance[source]=0;
intv=graph->edge[j].destination;
22
intweight=graph->edge[j].weight;
if(StoreDistance[u] +weight<StoreDistance[v])
StoreDistance[v] =StoreDistance[u]+weight;
}
}
//Actuallyuptonowshortestpathfound.ButBellmanFordchecksfornegative
edgecycle.Inthisstepwecheckforthat
//shortestdistancesifgraphdoesn'tcontainnegativeweightcycle.
//If
wegetashorterpath,
thenthereisanegativeedgecycle.
for(i=0;i<E;i++)
{
intu=graph->edge[i].source;
intv=graph->edge[i].destination;
intweight=graph->edge[i].weight;
if(StoreDistance[u]+weight<StoreDistance[v])
printf("Thisgraphcontainsnegativeedgecycle\n");
}
FinalSolution(StoreDistance, V);
return;
}
intmain()
{
intV,E,S;//V=no.ofVertices,E=no.ofEdges,Sissourcevertex
printf("Enternumberofverticesingraph\n");
scanf("%d",&V);
printf("Enternumberofedgesingraph\n");
scanf("%d",&E);
printf("Enteryoursourcevertexnumber\n");
scanf("%d",&S);
23
structGraph*graph=createGraph(V,E);//callingthefunctiontoallocate
spacetothesemanyverticesand
edges
inti;
for(i=0;i<E;i++){
printf("\nEnteredge%dpropertiesSource,destination,weight
respectively\n",i+1);
scanf("%d",&graph->edge[i].source);
scanf("%d",&graph->edge[i].destination);
scanf("%d",&graph->edge[i].weight);
}
BellmanFord(graph,S);
//passingcreatedgraphandsourcevertextoBellmanFordAlgorithmfunction
return0;
}
24
Program 9
Output
Enternumberofverticesingraph
5
Enternumberofedgesingraph
10
Enteryoursourcevertexnumber
0
Enteredge1propertiesSource,destination,
weight
respectively
016
Enteredge2propertiesSource,destination,
weight
respectively
027
Enteredge3propertiesSource,destination,
weight
respectively
128
Enteredge4propertiesSource,destination,
weight
respectively
14-4
Enteredge5propertiesSource,destination,
weight
respectively
135
Enteredge6propertiesSource,destination,
weight
respectively
31-2
Enteredge7propertiesSource,destination,
weight
respectively
23-3
25
Enteredge8propertiesSource,
destination,
weight
respectively
249
Enteredge9propertiesSource,
destination,
weight
respectively
402
Enteredge10propertiesSource,
destination,
weight
respectively
437
VertexDistancefromSourceVertex
00
12
27
34
4-2
TimeComplexity
WorstCase:O(|V|*|E|)
BestCase:O(|E|)
26
Program 10
Quick Sort
#include<stdio.h>
voidquick_sort(int[],int,int);
intpartition(int[],int,int);
intmain()
{
inta[50],n,i;
printf("Howmanyelements?");
scanf("%d",&n);
printf("\nEnterarrayelements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
quick_sort(a,0,n-1);
printf("\nArrayaftersorting:");
for(i=0;i<n;i++)
printf("%d",a[i]);
return0;
}
voidquick_sort(inta[],intl,intu)
{
intj;
if(l<u)
{
j=partition(a,l,u);
quick_sort(a,l,j-1);
quick_sort(a,j+1,u);
}
}
intpartition(inta[],intl,intu)
{
27
intv,i,j,temp;
v=a[l];
i=l;
j=u+1;
do
{
do
i++;
while(a[i]<v&&i<=u);
do
j--;
while(v<a[j]);
if(i<j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}while(i<j);
a[l]=a[j];
a[j]=v;
return(j);
}
28
Program 10
Output
Howmanyelements?
6
Enterarrayelemets:
12
7
11
9
3
15
ArrayafterSorting:
3
7
9
11
12
15
29
Program 11
Optimal Binary Tree
#include<stdio.h>
#include<conio.h>
#defineMAX10
voidmain()
{
charele[MAX][MAX];
intw[MAX][MAX],c[MAX][MAX],r[MAX][MAX],p[MAX],q[MAX];
inttemp=0,root,min,min1,n;
inti,j,k,b;
clrscr();
printf("Enterthenumberofelements:");
scanf("%d",&n);
printf("\n");
for(i=1;i<=n;i++)
{
printf("EntertheElementof%d:",i);
scanf("%d",&p[i]);
}
printf("\n");
for(i=0;i<=n;i++)
{
printf("EntertheProbabilityof%d:",i);
scanf("%d",&q[i]);
}
printf("W\t\tC\t\tR\n");
for(i=0;i<=n;i++)
{
for(j=0;j<=n;j++)
{
if(i==j)
{
w[i][j]=q[i];
c[i][j]=0;
r[i][j]=0;
printf("W[%d][%d]:%d\tC[%d][%d]:%d\tR[%d][%d]:
%d\n",i,j,w[i][j],i,j,c[i][j],i,j,r[i][j]);
}
30
}
}
printf("\n");
for(b=0;b<n;b++)
{
for(i=0,j=b+1;j<n+1&&i<n+1;j++,i++)
{
if(i!=j&&i<j)
{
w[i][j]=p[j]+q[j]+w[i][j-1];
min=30000;
for(k=i+1;k<=j;k++)
{
min1=c[i][k-1]+c[k][j]+w[i][j];
if(min>min1)
{
min=min1;
temp=k;
}
}
c[i][j]=min;
r[i][j]=temp;
}
printf("W[%d][%d]:%d\tC[%d][%d]:%d\tR[%d][%d]:
%d\n",i,j,w[i][j],i,j,c[i][j],i,j,r[i][j]);
}
printf("\n");
}
printf("Minimumcost=%d\n",c[0][n]);
root=r[0][n];
printf("Root=%d\n",root);
getch();
}
31
Program 11
Output
Enterthenumberofelements:6
EntertheElementof1:10
EntertheElementof2:3
EntertheElementof3:9
EntertheElementof4:2
EntertheElementof5:0
EntertheElementof6:10
EntertheProbabilityof0:5
EntertheProbabilityof1:6
EntertheProbabilityof2:4
EntertheProbabilityof3:4
EntertheProbabilityof4:3
EntertheProbabilityof5:8
EntertheProbabilityof6:0
W C R
W[0][0]:5 C[0][0]:0 R[0][0]: 0
W[1][1]:6 C[1][1]:0 R[1][1]: 0
W[2][2]:4 C[2][2]:0 R[2][2]: 0
W[3][3]:4 C[3][3]:0 R[3][3]: 0
W[4][4]:3 C[4][4]:0 R[4][4]: 0
W[5][5]:8 C[5][5]:0 R[5][5]: 0
W[6][6]:0 C[6][6]:0 R[6][6]: 0
W[0][1]:21C[0][1]:21R[0][1]: 1
W[1][2]:13C[1][2]:13R[1][2]: 2
W[2][3]:17C[2][3]:17R[2][3]: 3
W[3][4]:9 C[3][4]:9 R[3][4]: 4
W[4][5]:11C[4][5]:11R[4][5]: 5
32
W[5][6]:18C[5][6]:18R[5][6]:
6
W[0][2]:28C[0][2]:41R[0][2]:
1
W[1][3]:26C[1][3]:39R[1][3]:
3
W[2][4]:22C[2][4]:31R[2][4]:
3
W[3][5]:17C[3][5]:26R[3][5]:
5
W[4][6]:21C[4][6]:32R[4][6]:
6
W[0][3]:41C[0][3]:79R[0][3]:
2
W[1][4]:31C[1][4]:53R[1][4]:
3
W[2][5]:30C[2][5]:56R[2][5]:
3
W[3][6]:27C[3][6]:53R[3][6]:
6
W[0][4]:46C[0][4]:96R[0][4]:
3
W[1][5]:39C[1][5]:78R[1][5]:
3
W[2][6]:40C[2][6]:89R[2][6]:
4
W[0][5]:54C[0][5]:121 R[0][5]:
3
W[1][6]:49C[1][6]:115 R[1][6]:
3
W[0][6]:64C[0][6]:158 R[0][6]:
3
Minimumcost=158
Root=3
33