Professional Documents
Culture Documents
Ada Lab Programs
Ada Lab Programs
Write a program to search an element in an array using linear Soarch mothod. Find thetim
Program
required to search an element. Oulput
Enter the number
#includesstdio.h>
5
#include<time.h»
Enter the elemen
#include sstdlib.h> 10 20 30 40 50
int linearSearch (int a].int n,int key) Enter the key ele
50
int i; Thesearch elein=
The Execution Ti
for(i-0;isn;i++)
if (key == a[]) Program 2:,
return i;
#include<stdio.h:
return-1; //Unsuccessful Search - It returns -1 if key is not found #includectime.ha
finclude<stdlib.h
exit(0); else
else
elsE
printf("The search element is found at position %d\n",res+1);
printf("The Execution Time is %.0f Milli Seconds",time.taken);
=
return
Lab PnoçnaMs As
-thod. Find the time
Output
Enter the number of elements in the array:
Write a program to search an eloment in an orray using Binary Search mothod. Find the time
Program required to search an oloment.
#includeestdio.h>
#includectime.h>
#include<stdlib.h>
if (lastfirst)
return-1;
for(i=0;ic=n-2;i++)
for(j-0;j<=n-2-ij++),
if(ati+1]«ali)
temp=aj]:
a[il=alj+1];
ali+1]=temp;
while (first=last)
mid-(firstlast)/2;
if(tkey=afmid])
return mid+1;
else if(keysa[mid])
last=mid-1;
else
first=mid+1;
return 1;
Resiqw Ad Aálysis of Alcorirkiws E
i key-=a[mid]
return mid;
Write a program to search an element in an array using Rocursiva Binary Search mothod. Find
Program 3' tho timo roqvired to soarch an element.
#includesstdio.h>
#includectime.h>
#includestdlib.h>
int midi,.temp;
if(last first)
return-1;
for(i-0;ie=n-2;i++)
forj-0;j<=n-2-i:j++)
ifaj+1]<a[i])
temp=a[j];
a]=aj+1]:
aj+1]=temp;
mid-(first+ast)/2;
if(key==a[mid])
return mid;
else if(key<a{mid])
returnRBinarySearch(a,keyn,first,mid-1);
else
return RBinarySearch(a,keyn,mid+1,last)
void main0
char ch
int af100],n,key,l,res, first, last;
clock t st,et;
printf(Enter the number ofelements In the array: \n");
scanf("%d"&n);
printf("Enter the elements of the array:\n");
fori=0;i<n;i++)
scanf("%d"&a[i];
printf("Enter the key element to search: \n
scanf("%d",&key); .
first-0;
last=n-1;
Desiqw ANd ANalysis of Algonirkms
it+
iftres=-1) while (A[i]->=
printf"\nThe search element is not found \n");
printf("The Execution Time is =
%.0f Milli Seconds", time_taken);
if(i«)
exit(0); temp =
A[i] = A
else
printf(" nThe search element is found at position %d\n', res+1): AG] te
printf"The Execution Time is =%.0f Milli Seconds",time_taken);
Write d program to sort an elements in an array using Quick Sort. Find the time required printf Entert
Progrom 4 sort the elemenls.
to scanf("%d",&n
#include <stdio.h> printf Enter t
#include <conio.h> for ( 0;i< n;
#include <time.h>
SCanf%d
void quicksort(int A[10], int low, int high) st= clock0:
quicksort(A, 0
int j;
if (low < high)
et clock0:
double time_ta
jpartition(,low.high);
quicksort(A, low, j -1); printf("Sorted
quicksort[A, j + 1, high);
for(i 0;l<n
printf("
printf("The E=
getch0:
int partition(int A[10]. int low, int high)
temp A[pivot];
A[pivot) A0]:
Ali] = temp;
return j;
void main0
inti, n, A[10];
clockt st, et;
printf("Enter the number of elements of array: \n");
Quick Sort. Find tho time required to scanf("%d, &n);
printf("Enter the elements of the array: \n");
for (i = 0; i< n; i++)
scanf("%d", &A[):
st clock0:
quicksort(A, 0, n-1):
et clock0
double time taken = (((double) (et - st))/ CLOCKS_PER_SEC)*1000;
Output
Enter the number of elements of
array:
void Merge Sort(int A[10
5
Enter the elements int mid;
30 20 10 50 40
of the array:
Sorted list of elements : 10 20 30 40 50 if(lowehigh)
The Execution Time is = 0 in milli seconds
mid=(low+hig
Program 5
Write a
program
to sort an elements in an array using Merge Sort. Find the time
MergeSort(A
sort the elements. required to MergeSort(A
#include <stdio.h> Merge(Alow
#include <conio.h>
#include <time.h>
void Merge(int A[10],int lowint mid,int high)
void main00
int ij.k.c[10]:
{
i=low inti, n, A[10]:
k=low;
j=mid+1; clockt st, ct
while(i=mid)&&(j«=high)) printf"Enter the
if(A]<«=A[i}) scanf("%d", &n);
elkl=A;
kt+;
printf(The Exe
getch0
whiielj=high)
Output:
c[k=A[j]: Enter the number ofe
ktt;
++;
Enter the elements of
28 95 44 12
for(i=low; i<=k-1; i++) Sorted list of element
A[J=c[il; The Executton Timei=
HMs
Lab ProqRAMS
A.9
void MergcSortint A[10],int lowjnt high)
intmid;
if[lowehigh)
mid=(low+high)/2;
MergeSort(A,low.mid);
g Merge Sort. Find the lime
required to MergeSort{A,mid+1,high):
Merge(A,low.mid.high);
.
voidmain0
inti, n, A[10];
clockt st, et;
scanf("%d", &A[1]);
st clock0:
MergeSort(A, 0, n-1);
et =clock0:
double time_taken = (((double) (et- st))/ CLOCKS_PER_SEC)*1000;
Output
Enter the number ofelements of array
5
Enter the elements of the array:
289544 12
Sorted list of clements:59 12 28 44
The Execution Time is =0 in Milli Seconds
A Desiaw ANd Analysis.of Algooirhus
From a given verlex in a weighted connected graph fMnd shortost paths to othor vortices printf Enter the
Program 6 t usin
Dijikstras Algorilhm. scanf"%d",2v);
/* Shortest path using dljikstras algorithm"/
#include<stdio.h> Dijikstra(n,Mcost
void Dijikstrafint n,int v, int cost[10][10], int dist[]) printf"\n Shorte
for(j-1;j<=n; jt-
int countu,,wflag[10].min;
ifj=v)
for(i=1;ie=n;i+*) printf
{ getch0:
lagil=0;
dist[ij=cost[v][{];
Output
flagfv]=1; Enter the no of nodes
distv]=1;
count=2; Enter the cost matrix
0 60 100
whilefcount<=n)
min=9999:
for(w=1; w«=n; w++)
20
if(dist/w]« min && !fag[w])
min=dist[w], u=w;
Enter the source vert
flaglu=1;
countt+ Shortest path from
Progran 7 Fin
void main()
/ Minimum cost spa
int n,v.cost[10][10].dlst[10],1J; #indudestdio.h>
printf("\n Enter the no of nodes:");
int parent[10), min, n
scanf("%d",&n);.
int ab,ij.u,yn;
printf("\n Enter the cost matrix: \");
for(l=i;j«=n;i++) main0
forlj=1;je=n; j++) printf"\n En
scanf( %d",
scanf"%d",&cost[i][)]l):
if(cost[i]==0) printf"n Er
costfij]=9999; forfi=1;i=nz
forj=1-
. Lab PhoqnaMS,
Dijikstra(n,v.costdist);
printf"n Shortest path from \n");
for(j=1; j<=n; jt+)
if(jl=v)
printf\p%d->%d =%d"vj,dist[j]);
getch0:
Output
Enter the no of nodes:5
0 50 0
0 0 0 20
20 0 0
0 0 5 0
1->2 60
1->3 35
1->4 15
1->5 10
Find minimum cost spanning tree of a givon undirected graph using Kruskal's algorithm.
Progrom
|/* Minimum cost spanning tree uslng Kruskal's algorithm /
#include<stdio.h»
int parent10), min, no of.edges=1,mincost=0,cost[10][10];
int a,b,ij.u,y.n;
mainO
printf("\n Enter the no ofvertices:):
scanf("%d", &n);
printf"\n Enter the adjacency matrix: ");
for(i=1;i<=n;i++)
forj=1;j<=n;j++)
AJ2 Desicw adAnalysi of Alçogitns
Program 8 Find mi
scanf("%d"&cost[i)[i);
if(costill==0) /Minimum cost spannin
costfilf]=999; #include stdio.h>
while no ofedges < n) int a,b,u,v.n,ij.no_of.edge
intvisited[10), min, minc
0 11 9 7
11 0 15 14 13
9 15 0 12 14
14 12 0 6
8.13 14 6
Edge(4,5) =6
COSt
2 Edge(1,4)=7
3 Edge(13)=9
4 Edge(1,2)= 11 printf("\n
Minímum cost= 33
getch():
Lab PaóqnaMS
lGogithns
Find minimum cost spanning tree of a given undirected graph using Prims algorithm
Program8
/* Minimum cost spanning
tree using prims algorithm */
¥includesstdio.h>
int a,b,u,vn,i,f.no_of_edges=1;
visited[10], min, mincost=0, cost[10][10];
int
void main)
printf("\n Enter the no of vertices: ");
scanf("%d",&n);
printf("\n Enter the adjacency matrix ");
for(i=1;1k=n;i++)
for(j-1;j<=n;j+*)
scanf("%d",&cost[ij0]};
if(cost[i]0]==0)
cost[ []=999;
for(i=2;ic=n;i++)
visited[i]=0;
gesa,bmin); printf("\n The edges of spanning tree are :\n");
visited[1]=1;
while(no_of edges < n)
min = costf]]:
a=u=i;
b=vj
iffvisited(u]==0 || visited [v]==0)
getch()
A4 DesiqN ANd Analysis of Algoeirkins
floyd0:
Output
Enter the no ofvertices and graph data: 4 forfi=1;i<=n;i++)
0 1 c]-0;
printf("\n The all pair
11
for(i=1;i<=n; i-+)
22
11 for(j-1:jc=n; j++)
printf"%6d',c
The edges of spanning tree are:
printf"\n)
Edge(1,4)=1 getch0
2 Edge(1,2)= 2
Edge(2,3) = 1
Output
Minimum cost = 4
Enter the number of verticie
Implement all pairs shortest paths problem using Floyd's algorithm. Enter the adjacency E
Program 93
/ All pair shortest paths problem using Floyd's algorithm"/ 10
#include<stdio.h> 0
for(i=1;i<=n;i++)
0-0;
printf\n The all pair shortest path: \):
for(i=1;i«=n; 1++)
Output
Enter the number ofverticies: 4
0 20
50 0
0 60 0
Program 11 Check whether a given graph is connectod or not connected using DFS method.
void dfs(int v)
visited[v]=1;
Iv]=label;
for(i=1; ic=n; i++)
if(alv][i] && visited[il==0)
dfs (i);
nt:
fori=1; ie=n; i++)
if([ == 0)
label++
for(k=1;k<=n; k++)
visited [i]=0;
dís ():
if(label> 1)
printf"n Disconnected Graph";
else
printf("\n Connected Graph");
Print all th
printf("\n Do you want to continue :"); Program 12: search
fMush(stdin):
scanf("%c",&ch); #include<stdio.h>
#include<conio.h»
while ch=='y'||ch=='y);
intq[10-1r-1;
printf("The adjacency natrix is: \n");
void qinsintx)
forfi 1;i <<n; i++)
{ if(f-1&&r=-1)
for(j 1;j «= n; j++)
printf( 63d"a[li]G)); f+*;
printf("\n"); g++r=x
else
componentlabel(); q+*r]=*
getch0;
int qdel0
Oulput
int x=q[f;
Enterthe no vertices:4
if=r)
Enter the adjacent vertices:1 3 fsr-1;
Doyou want to continue: Y else
0 0
0
Connected Graph
Noof connected elements:1
Vertex 1 belongs to component 1
Vertex 2 belongs to component 1
Vertex 3 belongs to component 1
Vertex 4 belongs to component
Print all the nodes reaeheble from a given starting node in a given digraph using breadth flrst
Program 12
search
#include<stdio.h>
#include<conio.h>
intq[10]=1,r-1;
voidqins(intx)
if(f=-1&8&r=-1)
f++;
9++r]=x;
else
gl++r]=x;
intqdel0
int x=q[f;
(f==r)
far-1;
else
f+
return x;
A Desicie aid AudraisofAlkonithns
void main0
Output
int u.s[i0]=[0).srcna[10}[10J,AJ; Enter no of verti
scanf"%d",&src); 1
The nodes that
s[srcj=1;
qins(src);
whileft=-1 && rl=-1)
u=qdel0:
printf(" %d"u);
Program 13:
fori-1;<=n;i++)
ifalul-=1 &ks|ij==0) / Checkfor node:
#includeestdio.h>
s[il=1; #incdudecconio.h=
qins()
ints{10]={0};
void dfs(int n,int a.
printf"\n The nodes that are reachable from %d are:\n',src);
for(i=1;i<=n;it+) intv
ifsl) sfu=1;
printf(" %0
printf("%d \n",i);
for(v=1;v
getch0: if(a[u]
dist
void main(0
int a[10][1
print("\n E
scanf("%cd"
printf\n I
of Alkonith Lab Progaams
Outpu
Enter no of vertices:
1 0 1
0 0 1
1 0 0
0 0
0
Enter the source vertex: 1
1 2 4
The nodes that are reachable from 1 are:
int si10]={0}
void inain(0
int a[10][10],n,src,j;
printf("\n Enter the nuniber of vertices(n) \un"
scanf"%l",&en);
print"\n Enter the adjacency matrlx\n");