DAA Lab Programs Final
DAA Lab Programs Final
DAA Lab Programs Final
Selection Sort
public class SelectionSort {
void sort(int arr[]){
int n = arr.length;
for(int i = 0; i<n -1; i++){
int min_id = i;
for(int j = i+1; j< n; j++)
if(arr[j] < arr[min_id])
min_id = j;
int temp = arr[min_id];
arr[min_id] = arr[i];
arr[i] = temp;
}
}
public static void main(String[] args){
Scanner in = new Scanner(System.in);
SelectionSort ss = new SelectionSort();
System.out.print("Enter the number of elements in the array: ");
int n = in.nextInt();
int arr[] = new int[n];
System.out.print("Enter the elements of the array:");
for(int i = 0;i<n ; i++)
arr[i] = in.nextInt();
System.out.print("Array before sorting:");
for(int i = 0; i< n; i++)
System.out.print(arr[i] + " ");
System.out.println();
ss.sort(arr);
System.out.print("Array after sorting:");
for(int i = 0; i<n;++i)
System.out.print(arr[i] + " ");
System.out.println();
}
}
Floyds Algorithm
import java.util.*;
class Floyds{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of vertices: ");
int n =sc.nextInt();
System.out.println("Enter the adj matrix:(enter 999 for infinity) ");
int adj[][] = new int[10][10];
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
adj[i][j] = sc.nextInt();
flyod(adj,n);
System.out.println("the all pair shoretst path is: ");
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
System.out.print(adj[i][j]+" ");
}
System.out.println();
}
}
static void flyod(int arr[][],int n){
for(int k=1;k<=n;k++)
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
arr[i][j] = min(arr[i][j],(arr[i][k]+arr[k][j]));
}
static int min(int a,int b){
if(a<b)
return a;
return b;
}
Sum of Subset
public class LP17_SumOfSubSubset {
static int count = 0;
static void subset(int cs, int k, int r, int[] x, int[] w, int d) {
x[k] = 1;
if (cs + w[k] == d) {
count++;
System.out.print("Solution " + count + ": {");
for (int i = 0; i < w.length; i++)
if (x[i] == 1) System.out.print(w[i] + (i < w.length - 1 ? ", " : ""));
System.out.println("}");
} else if (cs + w[k] + w[k + 1] <= d) subset(cs + w[k], k + 1, r - w[k], x,w,d);
if (cs + r - w[k] >= d && cs + w[k + 1] <= d) {
x[k] = 0;
subset(cs, k + 1, r - w[k], x, w, d);
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("No of elements in the set: ");
int n = sc.nextInt(); int[] w = new int[n]; int[] x = new int[n]; int sum = 0;
System.out.print("Enter the elements: ");
for (int i = 0; i < n; i++) {
w[i] = sc.nextInt();
sum += w[i];
}
System.out.print("Enter the desired sum: ");
int d = sc.nextInt();
System.out.println("Sum is: " + sum);
subset(0, 0, sum, x, w, d);
}
}