Skip to content

DAA lab 2024 Java solutions #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions Lab Programs in Java 2024/1-GCD.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import java.util.ArrayList;

public class GCD {

public static void main(String[] args) {
int n1 = 366, n2 = 60;
timefind(n1, n2);
}

public static int euclid_hcf(int n1, int n2) {
if (n2 == 0) {
return n1;
} else {
int rem = n1 % n2;
return euclid_hcf(n2, rem);
}
}

public static int consec_hcf(int a, int b) {
int cur_hcf = 1;
for (int i = 1; i <= b; i++) {
if (a % i == 0 && b % i == 0) {
cur_hcf = i;
}
}
return cur_hcf;
}

public static int midsch_hcf(int a, int b) {
int cur_hcf = 1;

ArrayList<Integer> fac1 = new ArrayList<Integer>();
ArrayList<Integer> fac2 = new ArrayList<Integer>();

int c1 = 2;
int c2 = 2;

while (a != 1) {
if ((a % c1) == 0) {
fac1.add(c1);
a = a / c1;
} else {
c1++;
}
}

while (b != 1) {
if ((b % c2) == 0) {
fac2.add(c2);
b = b / c2;
} else {
c2++;
}
}

for (Integer element : fac1) {
if (fac2.contains(element)) {
cur_hcf = cur_hcf * element;
fac2.remove(element);
}
}
return cur_hcf;
}

public static void timefind(int a, int b) {
long startTime1 = System.nanoTime();
int result1 = euclid_hcf(a, b);
long endTime1 = System.nanoTime();
long executionTime1 = endTime1 - startTime1;
System.out.println("\nEuclid HCF: " + result1);
System.out.println("Execution time: " + executionTime1 + " nanoseconds");

long startTime2 = System.nanoTime();
int result2 = consec_hcf(a, b);
long endTime2 = System.nanoTime();
long executionTime2 = endTime2 - startTime2;
System.out.println("\nConsecutive HCF: " + result2);
System.out.println("Execution time: " + executionTime2 + " nanoseconds");

long startTime3 = System.nanoTime();
int result3 = midsch_hcf(a, b);
long endTime3 = System.nanoTime();
long executionTime3 = endTime3 - startTime3;
System.out.println("\nMid School HCF: " + result3);
System.out.println("Execution time: " + executionTime3 + " nanoseconds");
}
}
34 changes: 34 additions & 0 deletions Lab Programs in Java 2024/10-Knapsack01.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
public class Knapsack {

// Function to solve the 0/1 Knapsack problem
public static int knapsack(int[] weights, int[] values, int capacity) {
int n = weights.length;
int[][] dp = new int[n + 1][capacity + 1];

// Build the DP table
for (int i = 0; i <= n; i++) {
for (int w = 0; w <= capacity; w++) {
if (i == 0 || w == 0) {
dp[i][w] = 0;
} else if (weights[i - 1] <= w) {
dp[i][w] = Math.max(values[i - 1] + dp[i - 1][w - weights[i - 1]], dp[i - 1][w]);
} else {
dp[i][w] = dp[i - 1][w];
}
}
}

return dp[n][capacity];
}

public static void main(String[] args) {
int[] weights = { 1, 3, 4, 5 };
int[] values = { 1, 4, 5, 7 };
int capacity = 7;

// Calculate the maximum value that can be obtained
int maxValue = knapsack(weights, values, capacity);
// Print the maximum value
System.out.println("Maximum value in Knapsack = " + maxValue);
}
}
37 changes: 37 additions & 0 deletions Lab Programs in Java 2024/2-Binary Search.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
class BinarySearch {

// Method to perform binary search
public static int binarySearch(int[] arr, int target) {
int left = 0;
int right = arr.length - 1;

while (left <= right) {
int mid = left + (right - left) / 2;

if (arr[mid] == target) {
return mid; // Target found, return its index
}

if (arr[mid] < target) {
left = mid + 1; // Target is in the right half
} else {
right = mid - 1; // Target is in the left half
}
}

return -1; // Target not found, return -1
}

public static void main(String[] args) {
int[] numbers = {1, 2, 5, 5, 6, 9}; // Array must be sorted for binary search
int target = 5;

int result = binarySearch(numbers, target);

if (result == -1) {
System.out.println("Target " + target + " not found in the array.");
} else {
System.out.println("Target " + target + " found at index " + result + ".");
}
}
}
25 changes: 25 additions & 0 deletions Lab Programs in Java 2024/2-Linear Search.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class LinearSearch {

// Method to perform linear search
public static int linearSearch(int[] arr, int target) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] == target) {
return i; // Target found, return its index
}
}
return -1; // Target not found, return -1
}

public static void main(String[] args) {
int[] numbers = {5, 2, 9, 1, 5, 6};
int target = 5;

int result = linearSearch(numbers, target);

if (result == -1) {
System.out.println("Target " + target + " not found in the array.");
} else {
System.out.println("Target " + target + " found at index " + result + ".");
}
}
}
78 changes: 78 additions & 0 deletions Lab Programs in Java 2024/3-QuickSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import java.util.Random;

public class QuickSort {
// Function to swap two elements
private static void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}

private static void quickSort(int[] arr, int low, int high) {
if (low < high)
{
// Call Partition function to find Partition Index
// int partitionIndex = partition(arr, low, high);
int pivot = arr[low];
int i = low;
int j = high;

while (i < j) {
// Condition 1: find the first element greater than the pivot (from starting)
while (arr[i] <= pivot && i <= high - 1) {
i++;
}
// Condition 2: find the first element smaller than the pivot (from last)
while (arr[j] > pivot && j >= low + 1) {
j--;
}
if (i < j) {
swap(arr, i, j);
}
}
swap(arr, low, j);
// Recursively call quickSort() for left and right half based on partition Index
quickSort(arr, low, j - 1);
quickSort(arr, j + 1, high);
}
}

// Driver code
public static void main(String[] args) {
int n, l = 1;
long s, e;
double t;

java.util.Scanner sc = new java.util.Scanner(System.in);
System.out.print("How many elements in array?: ");
n = sc.nextInt();
int[] arr = new int[1000];
Random rand = new Random();
for (int i = 0; i < n; i++) {
// arr[i] = rand.nextInt(10) + 1;
arr[i] = rand.nextInt(n - l + 1);
}

// Printing the original array
System.out.print("Original array: ");
for (int i = 0; i < n; i++) {
System.out.print(arr[i] + " ");
}

// Calling quickSort() to sort the given array
s = System.nanoTime();
quickSort(arr, 0, n - 1);
e = System.nanoTime();

// Printing the sorted array
System.out.print("\nSorted array: ");
for (int i = 0; i < n; i++) {
System.out.print(arr[i] + " ");
}
t = (double)(e - s) / 1_000_000_000;
System.out.printf("\nThe time taken by pivot at 1st position: %f seconds\n", t);

sc.close();
}
}

76 changes: 76 additions & 0 deletions Lab Programs in Java 2024/4-MergeSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import java.util.Arrays;

public class MergeSort {
public static void main(String[] args) {
int[] array = {12, 11, 13, 5, 6, 7};
System.out.println("Original array: " + Arrays.toString(array));

mergeSort(array, 0, array.length - 1);

System.out.println("Sorted array: " + Arrays.toString(array));
}

public static void mergeSort(int[] array, int left, int right) {

if(left==right)
{
//base case
return;
}
else{
int mid = (left + right) / 2;

mergeSort(array, left, mid);
mergeSort(array, mid + 1, right);

//by the time we reach this line, only have singular nodes that have to be recursively merged together.
//so just before popping off the call stack, sort correctly and merge
merge(array, left, mid, right);
}
}

public static void merge(int[] array, int left, int mid, int right) {

//this code is the same as merging 2 sorted arrays

int n1 = mid - left + 1;
//size of left subarray

int n2 = right - mid;
// size fo right subarray

int[] leftArray = new int[n1];
int[] rightArray = new int[n2];

for (int i = 0; i < n1; i++)
leftArray[i] = array[left + i];
for (int j = 0; j < n2; j++)
rightArray[j] = array[mid + 1 + j];

int i = 0, j = 0;
int k = left;

while (i < n1 && j < n2) {
if (leftArray[i] <= rightArray[j]) {
array[k] = leftArray[i];
i++;
} else {
array[k] = rightArray[j];
j++;
}
k++;
}

while (i < n1) {
array[k] = leftArray[i];
i++;
k++;
}

while (j < n2) {
array[k] = rightArray[j];
j++;
k++;
}
}
}
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy