DAA Practical Code
DAA Practical Code
int n = sc.nextInt();
board[i][j] = 'x';
System.out.print("Enter the row for the first queen (0 to " + (n - 1) + "): ");
fixedRow = sc.nextInt();
System.out.print("Enter the column for the first queen (0 to " + (n - 1) + "): ");
fixedCol = sc.nextInt();
board[fixedRow][fixedCol] = 'Q';
cols[fixedCol] = true;
public sta c void nQueens(char[][] board, int row, boolean[] cols, boolean[] mainDiagonal,
boolean[] an Diagonal) {
if (row == board.length) {
printBoard(board);
count++;
return;
if (row == fixedRow) {
return;
}
// Try placing a queen in each column of the current row
// Place queen
board[row][col] = 'Q';
board[row][col] = 'x';
System.out.println("-------------Solu on--------------");
System.out.println();
System.out.println("-----------------------------------");
}
[2]Fibonacci
public class fib {
stepCount++;
if (n <= 1) {
return n;
if (n <= 1) {
return n;
int a = 0, b = 1, c = 0;
c = a + b;
a = b;
b = c;
stepCount++;
}
return c;
System.out.println("Enter n : ");
int n = sc.nextInt();
System.out.println("1.Recursive ");
System.out.println("2.Itera ve ");
switch (choice) {
case 1:
stepCount = 0;
break;
case 2:
stepCount = 0;
break;
default:
break;
}}}
[3]Frac onal Knapsack
int n = sc.nextInt();
val[i] = sc.nextInt();
weight[i] = sc.nextInt();
int w = sc.nextInt();
// Close scanner
sc.close();
// Calculate value-to-weight ra o
ra o[i][0] = i;
int capacity = w;
double finalVal = 0;
finalVal += val[idx];
capacity -= weight[idx];
} else {
break;
}}
[4]0/1 knapsack
class KnapsackItem {
int weight;
int value;
this.weight = weight;
this.value = value;
class Node {
double bound;
this.level = level;
this.profit = profit;
this.weight = weight;
this.bound = bound;
return 0;
int j = u.level + 1;
totalWeight += items[j].weight;
bound += items[j].value;
j++;
if (j < n) {
return bound;
int n = weights.length;
}
Arrays.sort(items, (i1, i2) -> Double.compare((double) i2.value / i2.weight, (double) i1.value /
i1.weight));
queue.add(u);
int maxProfit = 0;
while (!queue.isEmpty()) {
if (node.level == -1) {
u.level = 0;
if (node.level == n - 1)
con nue;
maxProfit = v.profit;
queue.add(v);
}
queue.add(v);
return maxProfit;
int n = weights.length;
if (weights[i - 1] <= w) {
} else {
return dp[n][capacity];
// Example data
// User choice
int maxValue;
switch (choice) {
case 1:
break;
case 2:
break;
default:
scanner.close();
}}
[5]QuickSort
public sta c void quickSortDeterminis c(int[] arr, int low, int high) {
quickSortRandomized(arr, pi + 1, high);
private sta c int par on(int[] arr, int low, int high) {
i++;
// swap arr[i] and arr[j]
arr[i] = arr[j];
arr[j] = temp;
arr[i + 1] = arr[high];
arr[high] = temp;
return i + 1;
private sta c int randomizedPar on(int[] arr, int low, int high) {
arr[randomIndex] = arr[high];
arr[high] = temp;
System.out.println();
}
public sta c void main(String[] args) {
int n = scanner.nextInt();
arr[i] = scanner.nextInt();
System.out.println("2. Randomized");
switch (choice) {
case 1:
break;
case 2:
break;
default:
return;
System.out.println("Sorted array:");
printArray(arr);
scanner.close();
}
[6]Huffman Coding
class MinHeapNode {
char data;
int freq;
MinHeapNode le , right;
le = right = null;
this.data = data;
this.freq = freq;
class HuffmanCoding {
if (root == null) {
return;
if (root.data != '$') {
le = minHeap.poll();
right = minHeap.poll();
temp.le = le ;
temp.right = right;
minHeap.add(temp);
printCodes(minHeap.peek(), "");
}
public sta c void main(String[] args) {