DAA Lab Programs
DAA Lab Programs
1. 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. Demonstrate
using C++/Java how the brute force method works along with its time complexity analysis: worst case,
average case and best case.
Selection sort in C++
#include <iostream>
using namespace std;
// function to swap the the position of two elements
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
// function to print an array
void printArray(int array[], int size) {
for (int i = 0; i < size; i++) {
cout << array[i] << " ";
}
cout << endl;
}
void selectionSort(int array[], int size) {
for (int step = 0; step < size - 1; step++) {
int min_idx = step;
for (int i = step + 1; i < size; i++) {
// To sort in descending order, change > to < in this line.
// Select the minimum element in each loop.
if (array[i] < array[min_idx])
min_idx = i;
}
import java.util.Arrays;
class SelectionSort {
void selectionSort(int array[]) {
int size = array.length;
// driver code
public static void main(String args[]) {
int[] data = { 20, 12, 10, 15, 2 };
SelectionSort ss = new SelectionSort();
ss.selectionSort(data);
System.out.println("Sorted Array in Ascending Order: ");
System.out.println(Arrays.toString(data));
}
}
2. 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. Demonstrate
using C++/Java how the divide-and-conquer method works along with its time complexity analysis: worst
case, average case and best case
import java.util.Random;
import java.util.Scanner;
class QuickSort {
quickSort(low, j - 1);
quickSort(j + 1, high);
while (i < j) {
comparisons += 1;
comparisons += 2;
i = i + 1;
comparisons += 2;
j = j - 1;
if (i < j) {
comparisons += 1;
interchange(i, j);
arr[low] = arr[j];
arr[j] = pivot;
return j;
arr[i] = arr[j];
arr[j] = temp;
int n;
n = scanner.nextInt();
System.out.println("Quick Sort");
int ch = scanner.nextInt();
switch (ch) {
case 1:
break;
case 2:
arr[i] = i + 1;
break;
quickSort(0, n - 1);
System.out.println("Sorted Array");
System.out.println(arr[i]);
3. . 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. Demonstrate
using C++/Java how the divide-and-conquer method works along with its time complexity analysis: worst
case, average case and best case.
import java.util.Scanner;
int n = in.nextInt();
for(int i=0;i<n;i++)
a[i]=rand.nextInt(2000);
System.out.println(a[i]);
start=System.nanoTime();
mergesort(a,0,n-1);
end=System.nanoTime();
System.out.println(a[i]);
mid = (low+high)/2;
static void merge(int a[], int low, int mid, int high)
b[i]=a[h];
h=h+1;
else
b[i] = a[j];
j=j+1;
i = i+1;
b[i]=a[k];
i= i+1;
else
for(k=h;k<=mid;k++)
b[i]=a[k];
i= i+1;
a[k] = b[k];
// Greedy approach
public class FractionalKnapSack {
return totalValue;
}
// Driver code
public static void main(String[] args)
{
// Function call
System.out.println(maxValue);
}
}
5. To find shortest paths to other vertices from a given vertex in a weighted connected graph, using Dijkstra's
algorithm
// A Java program for Dijkstra's single source shortest path
// algorithm. The program is for adjacency matrix
// representation of the graph
import java.io.*;
import java.lang.*;
import java.util.*;
class ShortestPath {
// A utility function to find the vertex with minimum
// distance value, from the set of vertices not yet
// included in shortest path tree
static final int V = 9;
int minDistance(int dist[], Boolean sptSet[])
{
// Initialize min value
int min = Integer.MAX_VALUE, min_index = -1;
// Driver's code
public static void main(String[] args)
{
/* Let us create the example graph discussed above
*/
int graph[][]
= new int[][] { { 0, 4, 0, 0, 0, 0, 0, 8, 0 },
{ 4, 0, 8, 0, 0, 0, 0, 11, 0 },
{ 0, 8, 0, 7, 0, 4, 0, 0, 2 },
{ 0, 0, 7, 0, 9, 14, 0, 0, 0 },
{ 0, 0, 0, 9, 0, 10, 0, 0, 0 },
{ 0, 0, 4, 14, 10, 0, 2, 0, 0 },
{ 0, 0, 0, 0, 0, 2, 0, 1, 6 },
{ 8, 11, 0, 0, 0, 0, 1, 0, 7 },
{ 0, 0, 2, 0, 0, 0, 6, 7, 0 } };
ShortestPath t = new ShortestPath();
// Function call
t.dijkstra(graph, 0);
}
}
6. To find Minimum Cost Spanning Tree of a given connected undirected graph using Kruskal's algorithm. Use
Union-Find algorithms in your program.
Time Complexity: O(E * logE) or O(E * logV)
/ Java program for Kruskal's algorithm
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
kruskals(V, graphEdges);
}
j++;
}
subsets[i].parent
= findRoot(subsets, subsets[i].parent);
return subsets[i].parent;
}
}
7. To find Minimum Cost Spanning Tree of a given connected undirected graph using Prim's algorithm.
/ A Java program for Prim's Minimum Spanning Tree (MST)
// algorithm. The program is for adjacency matrix
// representation of the graph
import java.io.*;
import java.lang.*;
import java.util.*;
class MST {
return min_index;
Compiled Swapna T ,CSE-NITR,Raichur
}
class AllPairShortestPath {
final static int INF = 99999, V = 4;
int i, j, k;
// Driver's code
public static void main(String[] args)
{
/* Let us create the following weighted graph
10
(0)------->(3)
| /|\
5| |
| |1
\|/ |
(1)------->(2)
3 */
int graph[][] = { { 0, 5, INF, 10 },
{ INF, 0, 3, INF },
{ INF, INF, 0, 1 },
{ INF, INF, INF, 0 } };
AllPairShortestPath a = new AllPairShortestPath();
// Function call
a.floydWarshall(graph);
}
}
System.out.println(
"The cost of most efficient tour = " + ans);
}
}
// Driver code
public static void main(String args[])
{
int profit[] = new int[] { 60, 100, 120 };
int weight[] = new int[] { 10, 20, 30 };
int W = 50;
int n = profit.length;
System.out.println(knapSack(W, weight, profit, n));
}
}
11. Design and implement C++/Java 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. For example, if S = {1, 2, 5, 6, 8}
and d= 9, there are two solutions {1, 2, 6} and {1, 8}. Display a suitable message, if the given problem
instance doesn't have a solution.
import java.io.*;
class GFG {
// Driver code
public static void main(String args[])
{
int set[] = { 1, 2, 5, 6, 8};
int sum = 9;
int n = set.length;
if (isSubsetSum(set, n, sum) == true)
System.out.println("Found a subset"
+ " with given sum");
else
System.out.println("No subset with"
+ " given sum");
}
}
12. Design and implement C++/Java Program to find all Hamiltonian Cycles in a connected undirected
Graph G of n vertices using backtracking principle.
return true;
}
printSolution(path);
return 1;
}