2022 Scheme Ada LAB Manual-1
2022 Scheme Ada LAB Manual-1
Design and implement C/C++ Program to sort a given set of n integer elements using
Selection Sort method and compute its time complexity. Run the program for varied values
of n> 5000 and record the time taken to sort. Plot a graph of the time taken versus n. The
elements can be read from a file or can be generated using the random number generator.
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
int a[50000],temp,i,j,min,n;
void selectionsort()
{
for(i=0;i<n-1;i++)
{
min=i;
for(j=i+1;j<n;j++)
if(a[j]<a[min])
min=j;
temp=a[i];
a[i]=a[min];
a[min]=temp;
}
}
void main()
{
clock_t start,end;
printf("Enter the number of Elements\n");
scanf("%d",&n);
printf("Reading elements using random number generator\n");
for(i=0;i<n;i++)
a[i]=rand();
start=clock();
selectionsort();
end=clock();
printf("The Sorted Element are\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
printf(" \nTime complexity=%f ",(double)(end-start)/(CLOCKS_PER_SEC));
OUTPUT:
Note: Run the program for varied values of n> 5000, and record the time taken to sort. Plot a
graph of the time taken versus n
2. Design and implement C/C++ Program to sort a given set of n integer elements using Merge
Sort method and compute its time complexity. Run the program for varied values of n> 5000,
and record the time taken to sort. Plot a graph of the time taken versus n. The elements can
be read from a file or can be generated using the random number generator.
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
int c[50000], a[50000];
void merge(int low, int mid, int high)
{
int i,k,j;
k=low;
i=low;
j=mid+1;
while((i<=mid)&&(j<=high))
{
if(a[i]<a[j])
c[k++]=a[i++];
else
c[k++]=a[j++];
}
while(i<=mid)
c[k++]=a[i++];
while(j<=high)
c[k++]=a[j++];
for(i=low;i<=k-1;i++)
a[i]=c[i];
}
void msort(int low,int high)
{
int mid;
if(low<high)
{
mid=(low+high)/2;
msort(low,mid);
msort(mid+1,high);
merge(low,mid,high);
}
}
void main()
{
int i,n;
clock_t start,end;
printf("Enter the number of Elements\n");
scanf("%d",&n);
printf("Reading elements using random number generator\n");
for(i=0;i<n;i++)
a[i]=rand();
start=clock();
msort(0,n-1);
end=clock();
printf("The Sorted Element are\n");
for(i=0;i<n;i++)
printf("%d\t",c[i]);
printf(" \nTime complexity=%f",(double)(end-start)/CLOCKS_PER_SEC);
}
OUTPUT:
Enter the number of Elements
5
Reading elements using random number generator
The Sorted Element are
846930886 1681692777 1714636915 1804289383 1957747793
Time complexity=0.000003
Note: Run the program for varied values of n> 5000, and record the time taken to sort. Plot a
graph of the time taken versus n
3. Design and implement C/C++ Program to sort a given set of n integer elements using Quick
Sort method and compute its time complexity. Run the program for varied values of n> 5000
and record the time taken to sort. Plot a graph of the time taken versus n. The elements can
be read from a file or can be generated using the random number generator.
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
int a[50000],n,i,j,temp;
int part(int low, int high)
{
int i,j,key;
key=a[low];
i=low+1;
j=high;
while(1)
{
while(i<high&&key>a[i])
i++;
while(a[j]>key)
j--;
if(i<j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
else
{
temp=a[low];
a[low]=a[j];
a[j]=temp;
return j;
}
}
}
void main()
{
clock_t start,end;
}
OUTPUT:
Enter the number of Elements
5
Reading elements using random number generator
The Sorted Element are
846930886 1681692777 1714636915 1804289383 1957747793
Time complexity=0.000001
Note: Run the program for varied values of n> 5000, and record the time taken to sort. Plot a
graph of the time taken versus n
4. Design and implement C/C++ Program to obtain the Topological ordering of vertices in a
given digraph.
#include <stdio.h>
#include <stdlib.h>
#define MAX_VERTICES 100
int adj[MAX_VERTICES][MAX_VERTICES];
int in_degree[MAX_VERTICES];
int visited[MAX_VERTICES];
int top_order[MAX_VERTICES];
int n;
void topological_sort()
{
int i, j, k = 0;
topological_sort();
return 0;
}
OUTPUT:
The algorithm is based on a direct implementation of the decrease-(by one)-and- conquer technique:
1. Repeatedly, identify in a remaining digraph a source, which is a vertex with no incoming edges,
and delete it along with all the edges outgoing from it. (If there are several sources, break the tie
arbitrarily. If there are none, stop because the problem cannot be solved.)
2. The order in which the vertices are deleted yields a solution to the topological sorting problem.
5. Design and implement C/C++ Program to find Minimum Cost Spanning Tree of a given connected
undirected graph using Kruskal's algorithm.
Program:
#include<stdio.h>
int parent[10], min, ne=1, mincost=0, cost[10][10];
int i,j,a,b,u,v,n;
void main()
{
printf("Enter the number of vertices\n”);
scanf("%d",&n);
printf(“Enter graph data\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;
}
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;
}
while(parent[u]) u=parent[u];
while(parent[v]) v=parent[v];
if(u!=v)
{
printf("\n%d\tEdge\t(%d,%d)=%d",ne++,a,b,min);
mincost+=min;
parent[v]=u;
}
cost[a][b]=cost[b][a]=999;
}
printf("\n\tMINCOST=%d\n",mincost);
}
OUTPUT:
Enter the number of vertices
6
Enter graph data
020240
203204
030107
221030
400300
047000
1 Edge (3,4)=1
2 Edge (1,2)=2
3 Edge (1,4)=2
4 Edge (4,5)=3
5 Edge (2,6)=4
MINCOST=12
MINCOST=12
Algorithm: