Posts

Literal table

 #code import java.util.*; class Literal {     String value;     int address;     public Literal(String value) {         this.value = value;         this.address = -1; // not assigned yet     } } public class Ass2{     // Check if a literal already exists in the list     public static boolean literalExists(List<Literal> list, String value) {         for (Literal l : list) {             if (l.value.equals(value)) return true;         }         return false;     }     public static void main(String[] args) {         Scanner scanner = new Scanner(System.in);         List<String> code = new ArrayList<>();         List<Literal> literalTable = new ArrayList<>();         Lis...

simple merge sort

 import java.util.Arrays; public class MergeSort {     // Merge sort function     public static void mergeSort(int[] array, int left, int right) {         if (left < right) {             // Find the middle point             int mid = (left + right) / 2;             // Recursively sort first and second halves             mergeSort(array, left, mid);             mergeSort(array, mid + 1, right);             // Merge the sorted halves             merge(array, left, mid, right);         }     }     // Merge function to merge two sorted halves     public static void merge(int[] array, int left, int mid, int right) {         // Sizes of two subarrays to be merged   ...

q3part b

  import java . util . Random ; public class TSPMutation {     public static void main( String [] args ) {         // Example cities (0, 1, 2, 3, 4)         int [] chromosome = { 0 , 1 , 2 , 3 , 4 }; // A possible solution (tour)                 System .out.println( "Original Chromosome:" );         printChromosome(chromosome);                 // Apply Mutation (Swap two cities in the path)         mutate(chromosome);                 System .out.println( "Mutated Chromosome:" );         printChromosome(chromosome);     }     // Mutation function: Swap two cities' positions     public static void mutate( int [] chromosome ) {         Random rand = new Random();           ...

MergeSort

  import java . util . Random ; public class MergesortExample {     public static void main( String [] args ) {         int N = 500 ; // Number of elements to sort         // Initialize an array of random integers         int [] arr = new int [N];         Random rand = new Random();         for ( int i = 0 ; i < N; i++) {             arr[i] = rand.nextInt( 1000 ); // Random integers between 0 and 999         }         // Displaying the unsorted array (optional)         System .out.println( "Unsorted array:" );         printArray(arr);         // Start measuring time         long startTime = System .nanoTime();         // Call the mergesort method         mergesort(arr, 0...

quicksort 1

 import java.util.Random; public class QuicksortExample {     public static void main(String[] args) {         int N = 500; // Number of elements to sort         // Initialize an array of random integers         int[] arr = new int[N];         Random rand = new Random();         for (int i = 0; i < N; i++) {             arr[i] = rand.nextInt(1000); // Random integers between 0 and 999         }         // Displaying the unsorted array (optional)         System.out.println("Unsorted array:");         printArray(arr);         // Start measuring time         long startTime = System.nanoTime();         // Call the quicksort method         quicksort(arr, 0, N - 1);       ...

multithread matrix

  public class MatrixMultiplicationMultithreaded {     public static void main( String [] args ) {         int N = 500 ; // Matrix size (N x N)                 // Initializing matrices A and B         int [][] A = new int [N][N];         int [][] B = new int [N][N];         int [][] C = new int [N][N]; // Result matrix         // Randomly initializing matrices A and B         initializeMatrix(A);         initializeMatrix(B);         // Multithreaded matrix multiplication         long startTime = System .nanoTime();         multiplyMatricesMultithreaded(A, B, C, N);         long endTime = System .nanoTime();                 long duration = (endTime - startTime); // ...

dinning phillosoper

  import java . util . concurrent .*; class DiningPhilosophers {     static final int NUM_PHILOSOPHERS = 5 ;         // Forks are shared resources, each philosopher will access them.     static Semaphore [] forks = new Semaphore [NUM_PHILOSOPHERS];         // Philosopher threads     static Philosopher [] philosophers = new Philosopher [NUM_PHILOSOPHERS];     public static void main( String [] args ) {         // Initialize the forks (semaphores)         for ( int i = 0 ; i < NUM_PHILOSOPHERS; i++) {             forks[i] = new Semaphore( 1 ); // 1 means each fork is initially available         }         // Create philosopher threads         for ( int i = 0 ; i < NUM_PHILOSOPHERS; i++) {             philosophers[i] = new Ph...