Professional Documents
Culture Documents
DAA Manual
DAA Manual
Prepared By :
Ms Meghana G H,
Associate Professor,
Dept of Computer Science and Engineering
Module 1
Program 1: Selection Sort
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.
ALGORITHM
1. k ← length [A]
2. for j ←1 to n-1
3. smallest ← j
4. for I ← j + 1 to k
5. if A [i] < A [ smallest]
6. then smallest ← i
7. exchange (A [j], A [smallest])
Program
package first;
import java.util.Scanner;
public class SelectionSort {
public static void main(String[] args) {
int size, i,j,temp;
int arr[] = new int[5000];
Scanner scan = new Scanner(System.in);
System.out.println("Enter Array Size:");
size = scan.nextInt();
System.out.println("Enter the Array Elements\t");
for(i=0; i<size; i++) {
arr[i] = scan.nextInt();
}
System.out.println("Sorting Array Using Selection Sort Technique...\t");
for(i=0;i<size;i++) {
for(j=i+1; j<size; j++) {
if(arr[i] > arr[j]) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
//Display The Sorted Array
System.out.println("Now the Array After Sorting is:\t");
for(i=0; i<size; i++) {
System.out.println(arr[i]+"");
}
}
}
Module 2
1. 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 on graph sheet. The elements can be read from a file or can be
generated using the random number generator. Demonstrate using 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;
import java.util.Arrays;
import java.util.Random;
System.out.println("Input Array:");
for (int i = 0; i < n; i++)
System.out.print(a[i] + " ");
// set start time
long startTime = System.nanoTime();
QuickSortAlgorithm(0, n - 1);
long stopTime = System.nanoTime();
long elapsedTime = stopTime - startTime;
System.out.println();
System.out.println("\nSorted Array:");
for (int i = 0; i < n; i++)
System.out.print(a[i] + " ");
System.out.println();
System.out.println();
System.out.println("Time Complexity in ms for
n=" + n + " is: " + (double) elapsedTime / 1000000);
}
2. 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 on graph sheet. The elements can be read
from a file or can be generated using the random number generator. Demonstrate using
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;
if (h > mid)
for (k = j; k <= high; k++)
b[i++] = a[k];
else
for (k = h; k <= mid; k++)
b[i++] = a[k];
import java.util.Scanner;
class KObject { // Knapsack object details
float w;
float p;
float r;
}
public class KnapsackGreedy2 {
static final int MAX = 20; // max. no. of objects
static int n; // no. of objects
static float M; // capacity of Knapsack
ReadObjects(obj);
Knapsack(obj);
scanner.close();
}
import java.util.*;
import java.util.Scanner;
int i, j;
cost = new int[MAX][MAX];
import java.util.Scanner;
visited[1] = 1;
while (ne < n) {
for (i = 1, min = 999; i <= n; i++)
for (j = 1; j <= n; j++)
if (cost[i][j] < min)
if (visited[i] != 0) {
min = cost[i][j];
a = u = i;
b = v = j;
}
if (visited[u] == 0 || visited[v] == 0) {
System.out.println("Edge" + ne++ + ":(" + a + "," + b + ")" +
"cost:" + min);
mincost += min;
visited[b] = 1;
}
cost[a][b] = cost[b][a] = 999;
}
System.out.println("\n Minimun cost" + mincost);
}
}
Module 4
Program 1: Floyd's
import java.util.Scanner;
import java.util.Scanner;
scanner.close();
}
void HamiltonianMethod(int k) {
while (true) {
NextValue(k, G, x, n);
if (x[k] == 0)
return;
if (k == n) {
for (int i = 1; i <= k; i++)
System.out.print(x[i] + " ");
System.out.println(x[1]);
System.out.println();
found = true;
return;
}
else
HamiltonianMethod(k + 1);
}
}