Ada Lab Manual
Ada Lab Manual
Department of CSE
(IoT & Cyber Security including Block Chain Technology)
March 12, 2025 Guru Nanak Dev Engineering College Mailoor Road Bidar-585401
WEBSITE: [Link] EMAIL: hodicb@[Link]
GURU NANAK DEV ENGINEERING COLLEGE, BIDAR-585401
Department of CSE
(IoT & Cyber Security including Block Chain Technology)
March 12, 2025 Guru Nanak Dev Engineering College Mailoor Road Bidar-585401
WEBSITE: [Link] EMAIL: hodicb@[Link]
GURU NANAK DEV ENGINEERING COLLEGE, BIDAR-585401
Department of CSE
(IoT & Cyber Security including Block Chain Technology)
M1: Provide a solid foundation in IoT, Cyber Security, and Blockchain Technology
through rigorous academic programs that blend theoretical knowledge with hands-on
practical experience.
March 12, 2025 Guru Nanak Dev Engineering College Mailoor Road Bidar-585401
WEBSITE: [Link] EMAIL: hodicb@[Link]
GURU NANAK DEV ENGINEERING COLLEGE, BIDAR-585401
Department of CSE
(IoT & Cyber Security including Block Chain Technology)
SYLLABUS
Subject: Analysis & Design of Algorithms Lab Semester: 4
Total Hours of Pedagogy: 40 hours Theory + 8-10 Lab slots Total Marks: 100
CO1: Apply asymptotic notational method to analyze the performance of the algorithms in
terms of time complexity.
CO2: Demonstrate divide & conquer approaches and decrease & conquer approaches to solve
computational problems using suitable tools.
CO3: Make use of transform & conquer and dynamic programming design approaches to solve
the given real world or complex computational problems.
CO4: Apply greedy and input enhancement methods to solve graph & string based
computational problems using suitable tools.
March 12, 2025 Guru Nanak Dev Engineering College Mailoor Road Bidar-585401
WEBSITE: [Link] EMAIL: hodicb@[Link]
GURU NANAK DEV ENGINEERING COLLEGE, BIDAR-585401
Department of CSE
(IoT & Cyber Security including Block Chain Technology)
Experiments
Experiment 1 Design and implement C/C++ Program to find Minimum Cost Spanning Tree of a
given connected undirected graph using Kruskal's algorithm.
Experiment 2 Design and implement C/C++ Program to find Minimum Cost Spanning Tree of a
given connected undirected graph using Prim's algorithm.
Experiment 3 a. Design and implement C/C++ Program to find the transitive closure using
Warshal's algorithm.
Experiment 4 Design and implement C/C++ Program to find shortest paths from a given vertex
in a weighted connected graph to other vertices using Dijkstra's algorithm.
Experiment 5 Design and implement C/C++ Program to obtain the Topological ordering of
vertices in a given digraph.
Experiment 6 Design and implement C/C++ Program to solve 0/1 Knapsack problem using
Dynamic Programming method.
Experiment 7 Design and implement C/C++ Program to solve discrete Knapsack and
continuous Knapsack problems using greedy approximation method.
Experiment 8 Design and implementC/C++ Program to find a subset of a given set S = {sl ,
s2,.....,sn} of n positive integers whose sum is equal to a given positive integer d.
Experiment 9 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.
Experiment 10 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.
March 12, 2025 Guru Nanak Dev Engineering College Mailoor Road Bidar-585401
WEBSITE: [Link] EMAIL: hodicb@[Link]
GURU NANAK DEV ENGINEERING COLLEGE, BIDAR-585401
Department of CSE
(IoT & Cyber Security including Block Chain Technology)
Experiment 11 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.
Experiment 12 Design and implement C/C++ Program for N Queen's problem using
Backtracking.
SOFTWARE REQUIREMENTS:
GCC COMPILER : The GNU Compiler Collection (GCC) is a collection of compilers that can be
used to run programs of various languages such as C++, C, Fortran and many more. It is an open-
source software developed by the GNU Project that may work on a variety of platforms,
including Windows, iOS, ARM based embedded systems, Linux, PowerPC and others.
NOTEPAD++ : Notepad++ is a free, open-source text and source code editor for Microsoft
Windows.
March 12, 2025 Guru Nanak Dev Engineering College Mailoor Road Bidar-585401
WEBSITE: [Link] EMAIL: hodicb@[Link]
GURU NANAK DEV ENGINEERING COLLEGE, BIDAR-585401
Department of CSE
(IoT & Cyber Security including Block Chain Technology)
Running the C program: (run the executible file) generated_file.exe ‘OR’ ./out
March 12, 2025 Guru Nanak Dev Engineering College Mailoor Road Bidar-585401
WEBSITE: [Link] EMAIL: hodicb@[Link]
GURU NANAK DEV ENGINEERING COLLEGE, BIDAR-585401
Department of CSE
(IoT & Cyber Security including Block Chain Technology)
Program 1: Design and implement C/C++ Program to find Minimum Cost Spanning Tree
of a given connected undirected graph using Kruskal's algorithm.
Aim : To apply Kruskal's Algorithm for computing the minimum spanning tree is directly based on the
generic MST algorithm. It builds the MST in forest.
Definition: Kruskal’s algorithm is an algorithm in graph theory that finds a minimum spanning tree for a
connected weighted graph. This mean it finds a subset of the edges that forms a tree that includes every
vertex, where the total weight of all the edges in the tree is minimized. If the graph is not connected, then
it finds a minimum spanning forest .It is an example of a greedy algorithm.
Efficiency:With an efficient sorting algorithm,the time efficiency of Kruskal’s algorithm will be in
O(│E│log│E│)
Algorithm:
Start with an empty set A, and select at every stage the shortest edge that has not been chosen or rejected,
regardless of where this edge is situated in graph.
Initially, each vertex is in its own tree in forest.
Then, algorithm consider each edge in turn, order by increasing weight.
If an edge (u, v) connects two different trees, then (u, v) is added to the set of edges of the MST, and two
trees connected by an edge (u, v) are merged into a single tree.
On the other hand, if an edge (u, v) connects two vertices in the same tree, then edge (u, v) is discarded.
Kruskals algorithm can be implemented using disjoint set data structure or priority queue data
structure.
Program:
#include<stdio.h>
#define INF 999
#define MAX 100
int p[MAX], c[MAX][MAX], t[MAX][2];
int find(int v)
{
March 12, 2025 Guru Nanak Dev Engineering College Mailoor Road Bidar-585401
WEBSITE: [Link] EMAIL: hodicb@[Link]
GURU NANAK DEV ENGINEERING COLLEGE, BIDAR-585401
Department of CSE
(IoT & Cyber Security including Block Chain Technology)
while (p[v])
v = p[v];
return v;
}
void union1(int i, int j)
{
p[j] = i;
}
void kruskal(int n)
{
int i, j, k, u, v, min, res1, res2, sum = 0;
for (k = 1; k < n; k++)
{
min = INF;
for (i = 1; i < n - 1; i++)
{
for (j = 1; j <= n; j++)
{
if (i == j) continue;
if (c[i][j] < min)
{
u = find(i);
v = find(j);
if (u != v)
{
res1 = i;
res2 = j;
min = c[i][j];
}
}
}
}
union1(res1, find(res2));
t[k][1] = res1;
t[k][2] = res2;
sum = sum + min;
}
printf("\nCost of spanning tree is=%d", sum);
printf("\nEdgesof spanning tree are:\n");
for (i = 1; i < n; i++)
printf("%d -> %d\n", t[i][1], t[i][2]);
}
int main()
{
March 12, 2025 Guru Nanak Dev Engineering College Mailoor Road Bidar-585401
WEBSITE: [Link] EMAIL: hodicb@[Link]
GURU NANAK DEV ENGINEERING COLLEGE, BIDAR-585401
Department of CSE
(IoT & Cyber Security including Block Chain Technology)
int i, j, n;
printf("\nEnter the n value:");
scanf("%d", & n);
for (i = 1; i <= n; i++)
p[i] = 0;
printf("\nEnter the graph data:\n");
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
scanf("%d", & c[i][j]);
kruskal(n);
return 0;
}
Output:
Enter the n value:5
Enter the graph data:
13462
17693
5 2 8 99 45
1 44 66 33 6
12 4 3 2 0
Cost of spanning tree is=11
Edgesof spanning tree are:
2 -> 1
1 -> 5
3 -> 2
1 -> 4
March 12, 2025 Guru Nanak Dev Engineering College Mailoor Road Bidar-585401
WEBSITE: [Link] EMAIL: hodicb@[Link]
GURU NANAK DEV ENGINEERING COLLEGE, BIDAR-585401
Department of CSE
(IoT & Cyber Security including Block Chain Technology)
Program 2: Design and implement C/C++ Program to find Minimum Cost Spanning Tree
of a given connected undirected graph using Prim's algorithm.
Aim:To find minimum spanning tree of a given graph using prim’s algorithm
Definition: Prim’s is an algorithm that finds a minimum spanning tree for a connected weighted
undirected graph. This means it finds a subset of the edges that forms a tree that includes every vertex,
where the total weight of all edges in the tree is minimized. Prim’s algorithm is an example of a greedy
algorithm.
Algorithm:
MST_PRIM (G, w, v)
1. Q ← V[G]
2. for each u in Q do
3. key [u] ← ∞
4. key [r] ← 0
5. π[r] ← NIl
6. while queue is not empty do
7. u ← EXTRACT_MIN (Q)
8. for each v in Adj[u] do
9. if v is in Q and w(u, v) < key [v]
10. then π[v] ← w(u, v)
11. key [v] ← w(u, v)
Analysis
The performance of Prim's algorithm depends of how we choose to implement the priority queue Q.
Program:
#include<stdio.h>
#define INF 999
int prim(int c[10][10],int n,int s)
{
int v[10],i,j,sum=0,ver[10],d[10],min,u;
for(i=1; i<=n; i++)
{
ver[i]=s;
d[i]=c[s][i];
v[i]=0;
}
v[s]=1;
for(i=1; i<=n-1; i++)
{
min=INF;
for(j=1; j<=n; j++)
if(v[j]==0 && d[j]<min)
{
March 12, 2025 Guru Nanak Dev Engineering College Mailoor Road Bidar-585401
WEBSITE: [Link] EMAIL: hodicb@[Link]
GURU NANAK DEV ENGINEERING COLLEGE, BIDAR-585401
Department of CSE
(IoT & Cyber Security including Block Chain Technology)
min=d[j];
u=j;
}
v[u]=1;
sum=sum+d[u];
printf("\n%d -> %d sum=%d",ver[u],u,sum);
for(j=1; j<=n; j++)
if(v[j]==0 && c[u][j]<d[j])
{
d[j]=c[u][j];
ver[j]=u;
}
}
return sum;
}
void main()
{
int c[10][10],i,j,res,s,n;
printf("\nEnter n value:");
scanf("%d",&n);
printf("\nEnter the graph data:\n");
for(i=1; i<=n; i++)
for(j=1; j<=n; j++)
scanf("%d",&c[i][j]);
printf("\nEnter the souce node:");
scanf("%d",&s);
res=prim(c,n,s);
printf("\nCost=%d",res);
getch();
}
Output:
Enter n value:4
Enter the graph data:
4521
7592
1769
0285
Enter the souce node:4
4 -> 1 sum=0
4 -> 2 sum=2
1 -> 3 sum=4
Cost=4
March 12, 2025 Guru Nanak Dev Engineering College Mailoor Road Bidar-585401
WEBSITE: [Link] EMAIL: hodicb@[Link]
GURU NANAK DEV ENGINEERING COLLEGE, BIDAR-585401
Department of CSE
(IoT & Cyber Security including Block Chain Technology)
Program 3: a. Design and implement C/C++ Program to solve All-Pairs Shortest Paths
problem using Floyd's algorithm.
b. Design and implement C/C++ Program to find the transitive closure using Warshal's
algorithm.
Definition: The Floyd algorithm is a graph analysis algorithm for finding shortest paths in a weighted
graph (with positive or negative edge weights). A single execution of the algorithm will find the lengths
(summed weights) of the shortest paths between all pairs of vertices though it does not return details of
the paths themselves. The algorithm is an example of dynamic programming
Algorithm:
Floyd’s Algorithm
Accept no .of vertices
Call graph function to read weighted graph // w(i,j)
Set D[ ] <- weighted graph matrix // get D {d(i,j)} for k=0
// If there is a cycle in graph, abort. How to find?
Repeat for k = 1 to n
Repeat for i = 1 to n
Repeat for j = 1 to n
D[i,j] = min {D[i,j], D[i,k] + D[k,j]}
Print D
Program A:
#include<stdio.h>
#include<conio.h>
#define INF 999
int min(int a,int b)
{
return(a<b)?a:b;
}
void floyd(int p[][10],int n)
{
int i,j,k;
for(k=1;k<=n;k++)
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
p[i][j]=min(p[i][j],p[i][k]+p[k][j]);
}
void main()
{
int a[10][10],n,i,j;
clrscr();
printf("\nEnter the n value:");
scanf("%d",&n);
printf("\nEnter the graph data:\n");
March 12, 2025 Guru Nanak Dev Engineering College Mailoor Road Bidar-585401
WEBSITE: [Link] EMAIL: hodicb@[Link]
GURU NANAK DEV ENGINEERING COLLEGE, BIDAR-585401
Department of CSE
(IoT & Cyber Security including Block Chain Technology)
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
floyd(a,n);
printf("\nShortest path matrix\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%d ",a[i][j]);
printf("\n");
}
getch();
}
Input/Output:
Enter the n value:4
Enter the graph data:
0 999 3 999
2 0 999 999
999 7 0 1
6 999 999 0
Shortest path matrix
0 10 3 4
2056
7701
6 16 9 0
Definition:The Floyd-Warshall algorithm is a graph analysis algorithm for finding shortest paths in a
weighted graph. A single execution of the algorithm will find the lengths of the shortest path between all
pairs of vertices though it does not return details of the paths themselves. The algorithm is an example of
Dynamic programming.
Algorithm
//Input: Adjacency matrix of digraph
//Output: R, transitive closure of digraph
Accept no .of vertices
Call graph function to read directed graph
Set R[ ] <- digraph matrix // get R {r(i,j)} for k=0
Print digraph
Repeat for k = 1 to n
Repeat for i = 1 to n
Repeat for j = 1 to n
R(i,j) = 1 if
{rij(k-1) = 1 OR
rik(k-1) = 1 and rkj(k-1) = 1}
Print R
March 12, 2025 Guru Nanak Dev Engineering College Mailoor Road Bidar-585401
WEBSITE: [Link] EMAIL: hodicb@[Link]
GURU NANAK DEV ENGINEERING COLLEGE, BIDAR-585401
Department of CSE
(IoT & Cyber Security including Block Chain Technology)
Program B:
#include<stdio.h>
void warsh(int p[][10],int n)
{
int i,j,k;
for(k=1;k<=n;k++)
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
p[i][j]=p[i][j] || p[i][k] && p[k][j];
}
int main()
{
int a[10][10],n,i,j;
printf("\nEnter the n value:");
scanf("%d",&n);
printf("\nEnter the graph data:\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
warsh(a,n);
printf("\nResultant path matrix\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%d ",a[i][j]);
printf("\n");
}
return 0;
}
Output:
Enter the n value:4
Enter the graph data:
0100
0001
0000
1010
Resultant path matrix
1111
1111
0000
1111
March 12, 2025 Guru Nanak Dev Engineering College Mailoor Road Bidar-585401
WEBSITE: [Link] EMAIL: hodicb@[Link]
GURU NANAK DEV ENGINEERING COLLEGE, BIDAR-585401
Department of CSE
(IoT & Cyber Security including Block Chain Technology)
Program 4: Design and implement C/C++ Program to find shortest paths from a given
vertex in a weighted connected graph to other vertices using Dijkstra's algorithm.
March 12, 2025 Guru Nanak Dev Engineering College Mailoor Road Bidar-585401
WEBSITE: [Link] EMAIL: hodicb@[Link]
GURU NANAK DEV ENGINEERING COLLEGE, BIDAR-585401
Department of CSE
(IoT & Cyber Security including Block Chain Technology)
March 12, 2025 Guru Nanak Dev Engineering College Mailoor Road Bidar-585401
WEBSITE: [Link] EMAIL: hodicb@[Link]
GURU NANAK DEV ENGINEERING COLLEGE, BIDAR-585401
Department of CSE
(IoT & Cyber Security including Block Chain Technology)
Program 5: Design and implement C/C++ Program to obtain the Topological ordering of
vertices in a given digraph
March 12, 2025 Guru Nanak Dev Engineering College Mailoor Road Bidar-585401
WEBSITE: [Link] EMAIL: hodicb@[Link]
GURU NANAK DEV ENGINEERING COLLEGE, BIDAR-585401
Department of CSE
(IoT & Cyber Security including Block Chain Technology)
if(a[i][j]==1)
id[j]++;
}
sort(a,id,n);
if(k!=n)
printf("\nTopological ordering not possible");
else
{
printf("\nTopological ordering is:");
for(i=1;i<=k;i++)
printf("%d ",temp[i]);
}
// getch();
}
Output:
Enter the n value:6
Enter the graph data:
001100
000110
000101
000001
000001
000000
Topological ordering is:1 2 3 4 5 6
March 12, 2025 Guru Nanak Dev Engineering College Mailoor Road Bidar-585401
WEBSITE: [Link] EMAIL: hodicb@[Link]
GURU NANAK DEV ENGINEERING COLLEGE, BIDAR-585401
Department of CSE
(IoT & Cyber Security including Block Chain Technology)
Program 6: Design and implement C/C++ Program to solve 0/1 Knapsack problem using
Dynamic Programming method.
March 12, 2025 Guru Nanak Dev Engineering College Mailoor Road Bidar-585401
WEBSITE: [Link] EMAIL: hodicb@[Link]
GURU NANAK DEV ENGINEERING COLLEGE, BIDAR-585401
Department of CSE
(IoT & Cyber Security including Block Chain Technology)
March 12, 2025 Guru Nanak Dev Engineering College Mailoor Road Bidar-585401
WEBSITE: [Link] EMAIL: hodicb@[Link]
GURU NANAK DEV ENGINEERING COLLEGE, BIDAR-585401
Department of CSE
(IoT & Cyber Security including Block Chain Technology)
Program 7: Design and implement C/C++ Program to solve discrete Knapsack and
continuous Knapsack problems using greedy approximation method.
This program first calculates the profit-to-weight ratio for each item, then sorts the items based on this
ratio in non-increasing order. It then fills the knapsack greedily by selecting items with the highest ratio
until the knapsack is full. If there's space left in the knapsack after selecting whole items, it adds fractional
parts of the next item. Finally, it prints the optimal solution and the solution vector.
Here's a simplified version of the C program to solve discrete Knapsack and continuous Knapsack
problems using the greedy approximation method:
Program:
#include <stdio.h>
#define MAX 50
int p[MAX], w[MAX], x[MAX];
double maxprofit;
int n, m, i;
void greedyKnapsack(int n, int w[], int p[], int m) {
double ratio[MAX];
// Calculate the ratio of profit to weight for each item
for (i = 0; i < n; i++) {
ratio[i] = (double)p[i] / w[i];
}
// Sort items based on the ratio in non-increasing order
for (i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (ratio[i] < ratio[j]) {
double temp = ratio[i];
ratio[i] = ratio[j];
ratio[j] = temp;
int temp2 = w[i];
w[i] = w[j];
w[j] = temp2;
temp2 = p[i];
p[i] = p[j];
p[j] = temp2;
}
}
}
int currentWeight = 0;
maxprofit = 0.0;
// Fill the knapsack with items
for (i = 0; i < n; i++) {
if (currentWeight + w[i] <= m) {
March 12, 2025 Guru Nanak Dev Engineering College Mailoor Road Bidar-585401
WEBSITE: [Link] EMAIL: hodicb@[Link]
GURU NANAK DEV ENGINEERING COLLEGE, BIDAR-585401
Department of CSE
(IoT & Cyber Security including Block Chain Technology)
int main() {
printf("Enter the number of objects: ");
scanf("%d", &n);
printf("Enter the objects' weights: ");
for (i = 0; i < n; i++)
scanf("%d", &w[i]);
printf("Enter the objects' profits: ");
for (i = 0; i < n; i++)
scanf("%d", &p[i]);
printf("Enter the maximum capacity: ");
scanf("%d", &m);
greedyKnapsack(n, w, p, m);
return 0;
}
OUTPUT
Enter the number of objects: 4
Enter the objects' weights: 56 78 98 78
Enter the objects' profits: 23 45 76 78
Enter the maximum capacity: 100
Optimal solution for greedy method: 78.0
Solution vector for greedy method: 1 0 0 0
Program 8: Design and implement C/C++ Program to find a subset of a given set S = {sl ,
March 12, 2025 Guru Nanak Dev Engineering College Mailoor Road Bidar-585401
WEBSITE: [Link] EMAIL: hodicb@[Link]
GURU NANAK DEV ENGINEERING COLLEGE, BIDAR-585401
Department of CSE
(IoT & Cyber Security including Block Chain Technology)
AIM: An instance of the Subset Sum problem is a pair (S, t), where S = {x1, x2, ..., xn} is a set of positive
integers and t (the target) is a positive integer. The decision problem asks for a subset of S whose sum is
as large as possible, but not larger than t.
Algorithm: SumOfSub (s, k, r)
//Values of x[ j ], 1 <= j < k, have been determined
//Node creation at level k taking place: also call for creation at level K+1 if possible
THEN { x[k]=0;
SumOfSub(s, k+1, r – s[k])
Program:
#include<stdio.h>
// #include<conio.h>
#define MAX 10
int s[MAX],x[MAX],d;
void sumofsub(int p,int k,int r)
{
int i;
x[k]=1;
if((p+s[k])==d)
{
for(i=1;i<=k;i++)
if(x[i]==1)
printf("%d ",s[i]);
printf("\n");
}
else
if(p+s[k]+s[k+1]<=d)
sumofsub(p+s[k],k+1,r-s[k]);
if((p+r-s[k]>=d) && (p+s[k+1]<=d))
{
x[k]=0;
sumofsub(p,k+1,r-s[k]);
March 12, 2025 Guru Nanak Dev Engineering College Mailoor Road Bidar-585401
WEBSITE: [Link] EMAIL: hodicb@[Link]
GURU NANAK DEV ENGINEERING COLLEGE, BIDAR-585401
Department of CSE
(IoT & Cyber Security including Block Chain Technology)
}
}
int main()
{
int i,n,sum=0;
printf("\nEnter the n value:");
scanf("%d",&n);
printf("\nEnter the set in increasing order:");
for(i=1;i<=n;i++)
scanf("%d",&s[i]);
printf("\nEnter the max subset value:");
scanf("%d",&d);
for(i=1;i<=n;i++)
sum=sum+s[i];
if(sum<d || s[1]>d)
printf("\nNo subset possible");
else
sumofsub(0,1,sum);
return 0;
}
Output:
Enter the n value:9
Enter the set in increasing order:1 2 3 4 5 6 7 8 9
Enter the max subset value:9
126
135
18
234
27
36
45
9
March 12, 2025 Guru Nanak Dev Engineering College Mailoor Road Bidar-585401
WEBSITE: [Link] EMAIL: hodicb@[Link]
GURU NANAK DEV ENGINEERING COLLEGE, BIDAR-585401
Department of CSE
(IoT & Cyber Security including Block Chain Technology)
Program 9: 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.
Aim: Sort a given set of elements using Selection sort and determine the time required to sort elements.
Repeat the experiment for different values of n, the number of elements in the list to be sorted and plot a
graph of the time taken versus n.
Definition: selection sort is a sorting routine that scans a list of items repeatedly and, on each pass,
selects the item with the lowest value and places it in its final position. It is based on brute force
approach. Sequential search is a Θ(n2) algorithm on all inputs.
Algorithm:
SelectionSort(A[0…n-1])
//sort a given array by select5ion sort
//input:A[0…n-1]of orderable elements
Output:Array a[0…n-1] Sorted in ascending order
for i<- 0 to n-2 do
min<-i
for j<-i+1 to n-1 do
if A[j]<A[min] min<-j
swap A[i] and A[min]
program:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
March 12, 2025 Guru Nanak Dev Engineering College Mailoor Road Bidar-585401
WEBSITE: [Link] EMAIL: hodicb@[Link]
GURU NANAK DEV ENGINEERING COLLEGE, BIDAR-585401
Department of CSE
(IoT & Cyber Security including Block Chain Technology)
arr[min_idx] = arr[i];
arr[i] = temp;
}
}
// Function to generate an array of random numbers
void generateRandomNumbers(int arr[], int n)
{
for (int i = 0; i < n; i++)
{
arr[i] = rand() % 10000; // Generate random numbers between 0 and 9999
}
}
int main()
{
int n;
printf("Enter number of elements: ");
scanf("%d", &n); // Read the number of elements from the user
if (n <= 5000)
{
printf("Please enter a value greater than 5000\n");
return 1; // Exit if the number of elements is not greater than 5000
}
// Allocate memory for the array
int *arr = (int *)malloc(n * sizeof(int));
if (arr == NULL)
{
printf("Memory allocation failed\n");
return 1; // Exit if memory allocation fails
}
March 12, 2025 Guru Nanak Dev Engineering College Mailoor Road Bidar-585401
WEBSITE: [Link] EMAIL: hodicb@[Link]
GURU NANAK DEV ENGINEERING COLLEGE, BIDAR-585401
Department of CSE
(IoT & Cyber Security including Block Chain Technology)
March 12, 2025 Guru Nanak Dev Engineering College Mailoor Road Bidar-585401
WEBSITE: [Link] EMAIL: hodicb@[Link]
GURU NANAK DEV ENGINEERING COLLEGE, BIDAR-585401
Department of CSE
(IoT & Cyber Security including Block Chain Technology)
Program 10: 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.
Aim:
The aim of this program is to sort ‘n’ randomly generated elements using Quick sort and Plotting the
graph of the time taken to sort n elements versus n.
Definition: Quick sort is based on the Divide and conquer approach. Quick sort divides array according to
their value. Partition is the situation where all the elements before some position s are smaller than or
equal to A[s] and all the elements after position s are greater than or equal to A[s].
Efficiency: Cbest(n) Є Θ(nlog2n),Cworst(n) ЄΘ(n2),Cavg(n)Є1.38nlog2n
Algorithm: Quick sort (A[l….r])
// Sorts a sub array by quick sort
//Input : A sub array A[l..r] of A[0..n-1] ,defined by its left and right indices l
//and r
// Output : The sub array A[l..r] sorted in non decreasing order
if l < r
s = Partition (A[l..r]) //s is a split position
Quick sort (A[l …s-1])
Quick sort (A[s+1…r])
ALGORITHM Partition (A[l…r])
//Partition a sub array by using its first element as a pivot
// Input : A sub array A[l…r] of A[0…n-1] defined by its left and right indices l and // r (l < r)
// Output : A partition of A[l…r], with the split position returned as this function’s value
p=A[l]
i=l;
j=r+1;
repeat
delay(500);
repeat i= i+1 until A[i] >= p
repeat j=j-1 until A[J] <= p
Swap (A[i],A[j])
until I >=j
Swap (A[i],A[j]) // Undo last Swap when i>= j
Swap (A[l],A[j])
Return j
Program:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// Function to swap two elements
March 12, 2025 Guru Nanak Dev Engineering College Mailoor Road Bidar-585401
WEBSITE: [Link] EMAIL: hodicb@[Link]
GURU NANAK DEV ENGINEERING COLLEGE, BIDAR-585401
Department of CSE
(IoT & Cyber Security including Block Chain Technology)
March 12, 2025 Guru Nanak Dev Engineering College Mailoor Road Bidar-585401
WEBSITE: [Link] EMAIL: hodicb@[Link]
GURU NANAK DEV ENGINEERING COLLEGE, BIDAR-585401
Department of CSE
(IoT & Cyber Security including Block Chain Technology)
int main()
{
int n;
printf("Enter number of elements: ");
scanf("%d", &n); // Read the number of elements from the user
if (n <= 5000)
{
printf("Please enter a value greater than 5000\n");
return 1; // Exit if the number of elements is not greater than 5000
}
// Allocate memory for the array
int *arr = (int *)malloc(n * sizeof(int));
if (arr == NULL)
{
printf("Memory allocation failed\n");
return 1; // Exit if memory allocation fails
}
March 12, 2025 Guru Nanak Dev Engineering College Mailoor Road Bidar-585401
WEBSITE: [Link] EMAIL: hodicb@[Link]
GURU NANAK DEV ENGINEERING COLLEGE, BIDAR-585401
Department of CSE
(IoT & Cyber Security including Block Chain Technology)
[Link]()
OUTPUT
Enter number of elements: 10000
Time taken to sort 10000 elements: 0.0000 seconds
Enter number of elements: 20000
Time taken to sort 20000 elements: 0.015000 seconds
Enter number of elements: 30000
Time taken to sort 30000 elements: 0.011000 seconds
Enter number of elements: 35000
Time taken to sort 35000 elements: 0.003000 seconds
Enter number of elements: 50000
Time taken to sort 50000 elements: 0.015000 seconds
March 12, 2025 Guru Nanak Dev Engineering College Mailoor Road Bidar-585401
WEBSITE: [Link] EMAIL: hodicb@[Link]
GURU NANAK DEV ENGINEERING COLLEGE, BIDAR-585401
Department of CSE
(IoT & Cyber Security including Block Chain Technology)
Program 11 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.
Aim: The aim of this program is to sort ‘n’ randomly generated elements using Merge sort and Plotting
the graph of the time taken to sort n elements versus n.
Definition: Merge sort is a sort algorithm based on divide and conquer technique. It divides the array
element based on the position in the array. The concept is that we first break the list into two smaller
lists of roughly the same size, and then use merge sort recursively on the subproblems, until they
cannot subdivide anymore. Then, we can merge by stepping through the lists in linear time. Its time
efficiency is Θ(n log n).
Algorithm: Merge sort (A[0…n-1]
// Sorts array A[0..n-1] by Recursive merge sort
// Input : An array A[0..n-1] elements
// Output : Array A[0..n-1] sorted in non decreasing order
If n > 1
Copy A[0…(n/2)-1] to B[0…(n/2)-1]
Copy A[0…(n/2)-1] to C[0…(n/2)-1]
Mergesort (B[0…(n/2)-1])
Mergesort (C[0…(n/2)-1])
Merge(B,C,A)
ALGORITHM Merge (B[0…p-1], C[0…q-1],A[0….p+q-1])
// merges two sorted arrays into one sorted array
March 12, 2025 Guru Nanak Dev Engineering College Mailoor Road Bidar-585401
WEBSITE: [Link] EMAIL: hodicb@[Link]
GURU NANAK DEV ENGINEERING COLLEGE, BIDAR-585401
Department of CSE
(IoT & Cyber Security including Block Chain Technology)
rogram:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// Function to merge two sorted arrays
void merge(int arr[], int left, int mid, int right)
{
int i, j, k;
int n1 = mid - left + 1;
int n2 = right - mid;
int *L = (int *)malloc(n1 * sizeof(int));
int *R = (int *)malloc(n2 * sizeof(int));
for (i = 0; i < n1; i++)
L[i] = arr[left + i];
for (j = 0; j < n2; j++)
R[j] = arr[mid + 1 + j];
i = 0;
j = 0;
k = left;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
while (j < n2)
March 12, 2025 Guru Nanak Dev Engineering College Mailoor Road Bidar-585401
WEBSITE: [Link] EMAIL: hodicb@[Link]
GURU NANAK DEV ENGINEERING COLLEGE, BIDAR-585401
Department of CSE
(IoT & Cyber Security including Block Chain Technology)
{
arr[k] = R[j];
j++;
k++;
}
free(L);
free(R);
}
// Function to implement Merge Sort
void mergeSort(int arr[], int left, int right)
{
if (left < right)
{
int mid = left + (right - left) / 2;
mergeSort(arr, left, mid);
mergeSort(arr, mid + 1, right);
merge(arr, left, mid, right);
}
}
// Function to generate random integers
void generateRandomArray(int arr[], int n)
{
for (int i = 0; i < n; i++)
arr[i] = rand() % 100000; // Generate random integers between 0 and 99999
}
int main()
{
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
if (n <= 5000)
{
printf("Please enter a value greater than 5000\n");
March 12, 2025 Guru Nanak Dev Engineering College Mailoor Road Bidar-585401
WEBSITE: [Link] EMAIL: hodicb@[Link]
GURU NANAK DEV ENGINEERING COLLEGE, BIDAR-585401
Department of CSE
(IoT & Cyber Security including Block Chain Technology)
generateRandomArray(arr, n);
// Repeat the sorting process multiple times to increase duration for timing
clock_t start = clock();
for (int i = 0; i < 1000; i++)
{
mergeSort(arr, 0, n - 1);
}
clock_t end = clock();
// Calculate the time taken for one iteration
double time_taken = ((double)(end - start)) / CLOCKS_PER_SEC / 1000.0;
printf("Time taken to sort %d elements: %f seconds\n", n, time_taken);
free(arr);
return 0;
}
O
March 12, 2025 Guru Nanak Dev Engineering College Mailoor Road Bidar-585401
WEBSITE: [Link] EMAIL: hodicb@[Link]
GURU NANAK DEV ENGINEERING COLLEGE, BIDAR-585401
Department of CSE
(IoT & Cyber Security including Block Chain Technology)
OUTPUT
Enter number of elements: 6000
Time taken to sort 6000 elements: 0.000709 seconds
Enter number of elements: 7000
Time taken to sort 7000 elements: 0.000752 seconds
P
r
o
March 12, 2025 Guru Nanak Dev Engineering College Mailoor Road Bidar-585401
WEBSITE: [Link] EMAIL: hodicb@[Link]
GURU NANAK DEV ENGINEERING COLLEGE, BIDAR-585401
Department of CSE
(IoT & Cyber Security including Block Chain Technology)
gram 12 Design and implement C/C++ Program for N Queen's problem using
Backtracking.
March 12, 2025 Guru Nanak Dev Engineering College Mailoor Road Bidar-585401
WEBSITE: [Link] EMAIL: hodicb@[Link]
GURU NANAK DEV ENGINEERING COLLEGE, BIDAR-585401
Department of CSE
(IoT & Cyber Security including Block Chain Technology)
eturn 1;
}
void display(int c[],int n)
{
int i,j;
char cb[10][10];
for(i=0;i<n;i++)
for(j=0;j<n;j++)
cb[i][j]='-';
for(i=0;i<n;i++)
cb[i][c[i]]='Q';
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
printf("%c",cb[i][j]);
printf("\n");
}
}
void n_queens(int n)
{
int r;
int c[MAX];
c[0]=-1;
r=0;
while(r>=0)
{ c[r]++;
while(c[r]<n && !can_place(c,r))
c[r]++;
if(c[r]<n)
{ if(r==n-1)
{ display(c,n);
printf("\n\n");
}
else
{ r++;
c[r]=-1;
}
}
else
r--;
March 12, 2025 Guru Nanak Dev Engineering College Mailoor Road Bidar-585401
WEBSITE: [Link] EMAIL: hodicb@[Link]
GURU NANAK DEV ENGINEERING COLLEGE, BIDAR-585401
Department of CSE
(IoT & Cyber Security including Block Chain Technology)
}
void main()
{
int n;
clrscr();
printf("\nEnter the no. of queens:");
scanf("%d",&n);
n_queens(n);
getch();
}
Output:
Enter the no. of queens:4
-Q--
---Q
Q---
--Q-
--Q-
Q---
---Q
-Q—
March 12, 2025 Guru Nanak Dev Engineering College Mailoor Road Bidar-585401
WEBSITE: [Link] EMAIL: hodicb@[Link]