I MASTER OF COMPUTER APPLICATIONS SUBMITTED BY NAME : REG NO : SEMESTER : II – SEMESTER SUBJECT : ALGORITHMS LAB CODE : 25MCA2S1 DEPARTMENT OF COMPUTER APPLICATIONS ALAGAPPA UNIVERSITY (A State university Accredited with A+ Grade by NAAC (CGPA: 3.59) in the Fourth Cycle and Graded as Category - I University by MHRD – UGC) KARAIKUDI - 630003 APRIL - 2026 II ALAGAPPA UNIVERSITY (A State university Accredited with A+ Grade by NAAC (CGPA: 3.59) in the Fourth Cycle and Graded as Category - I University by MHRD – UGC) KARAIKUDI - 630003 DEPARTMENT OF COMPUTER APPLICATIONS CERTIFICATE REG NO : COURSE CODE : 541 COURSE : MCA Certified that this is the Bonafide record work done by in II - semester of MCA at the Department of Computer Applications, Alagappa University, Karaikudi – APRIL 2026. HEAD OF THE DEPARTMENT STAFF - IN - CHARGE Submitted for the university Practical examinations held on at the Department of Computer Applications, Alagappa University, Karaikudi. INTERNALEXAMINER EXTERNALEXAMINER III INDEX PAGE S.NO DATE TITLE PAGE NO SIGN 1 GCD AND LCM 01 2 FIBONACCI SERIES 04 3 SELECTION SORT 07 4 BUBBLE SORT 11 5 LINEAR SEARCH 15 6 BINARY SEARCH 19 7 STACK OPERATIONS USING ARRAY 23 8 BINOMIAL COEFFICIENT 27 9 WARSHALL ALGORITHM 30 10 FLOYD’S ALGORITHM 34 11 KNAPSACK PROBLRM 37 12 PRIM’S ALGORITHM 41 13 KRUSKAL’S ALGORITHM 45 14 TOPOLOGICAL ORDERING 49 15 BREADTH FIRST SEARCH 54 16 DEPTH FIRST SEARCH 58 17 QUICK SORT 62 18 MERGE SORT 66 19 SUBSET SUM PROBLEM 71 20 TRAVELLING SALESMAN PROBLEM 75 21 HAMILTONIAN CYCLE 79 1 AIM : To write a Java program to find the GCD and LCM of two given numbers ALGORITHM: Step 1: Start. Step 2: Declare variables x, y, gcd = 1, and lcm. Step 3: Read two numbers x and y. Step 4: Repeat the loop from I = 1 to I ≤ x and I ≤ y. Step 5: Check if x and y are divisible by i. Step 6: If both are divisible, store I as gcd. Step 7: Continue the loop until the condition ends. Step 8: Print the value of gcd. Step 9: Calculate lcm = (x × y) / gcd. Step 10: Print the value of lcm. Step 11: Stop. EX NO : 01 DATE : GCD AND LCM 2 PROGRAM: //gcd.java import java.util.Scanner; public class gcd { public static void main(String[] args) { int x , y , gcd = 1,lcm; Scanner sc=new Scanner(System.in); System.out.print("Enter X value:"); x=sc.nextInt(); System.out.print("Enter Y value:"); y=sc.nextInt(); for(int i = 1; i <= x && i <= y; i++) { if(x%i==0 && y%i==0) gcd = i; } System.out.println("GCD :"+ gcd); lcm=(x*y)/gcd; System.out.println("LCM :"+ lcm); } } 3 OUTPUT: RESULT: Thus the given program is Executed Successfully and Output is verified. 4 AIM: To write a Java program to generate Fibonacci series using recursion. ALGORITHM: Step 1: Start the program. Step 2: Define a recursive function fiboRecur(n). Step 3: If n = 0, return 0. Step 4: If n = 1 or n = 2, return 1. Step 5: Otherwise return fiboRecur(n - 1) + fiboRecur(n - 2). Step 6: In the main method, read the value N. Step 7: Use a loop from I = 0 to N - 1. Step 8: Call the recursive function and print Fibonacci numbers. Step 9: Stop the program. EX NO : 02 DATE : FIBONACCI SERIES 5 PROGRAM: //fibo.java import java.util.Scanner; public class fibo{ public static int fibbo(int n) { if(n == 0) { return 0; } if(n == 1 || n == 2) { return 1; } return fibbo(n - 2) + fibbo(n - 1); } public static void main(String args[]) { Scanner s=new Scanner(System.in); System.out.println("Enter Any Number: "); int n=s.nextInt(); System.out.println("Fibonacci Series: "); for(int i = 0; i < n; i++) { System.out.println(fibbo(i) +" "); } } } 6 OUTPUT: RESULT: Thus the given program is Executed Successfully and Output is verified. 7 AIM: To write a Java program to sort a set of numbers using Selection Sort. ALGORITHM: Step 1: Start the program. Step 2: Read the number of elements n. Step 3: Read n elements into an array. Step 4: Use a loop from I = 0 to n - 1. Step 5: Compare element arr[i] with remaining elements. Step 6: If a smaller element is found, swap the elements. Step 7: Repeat the process until the array is sorted. Step 8: Display the sorted array. Step 9: Stop the program. EX NO : 03 DATE : SELECTION SORT 8 PROGRAM: //SSort.java import java.util.Scanner; public class SSort { public static void selectionSort(int[] arr,int n){ for (int i = 0; i < n - 1; i++) { for (int j = i + 1; j < n; j++) { if (arr[i] > arr[j]) { int t = arr[i]; arr[i] = arr[j]; arr[j] = t; } } } } public static void main(String a[]) { Scanner sc=new Scanner(System.in); System.out.print("Enter the number of elements you want to store: "); int n=sc.nextInt(); int[] array = new int[n]; System.out.println("Enter the elements of the array: "); for(int i=0; i<n; i++) { array[i]=sc.nextInt(); } System.out.println("Before Selection Sort"); 9 for(int i=0; i<n; i++) { System.out.print(array[i]+" "); } System.out.println(); selectionSort(array,n); System.out.println("After Selection Sort"); for(int i=0; i<n; i++) { System.out.print(array[i]+" "); } } } 10 OUTPUT: RESULT: Thus the given program is Executed Successfully and Output is verified. 1 1 AIM: To write a Java program to sort a set of numbers using Bubble Sort. ALGORITHM: Step 1: Start the program. Step 2: Read the number of elements n. Step 3: Read the array elements. Step 4: Use outer loop from I = 0 to n - 1. Step 5: Use inner loop to compare adjacent elements. Step 6: If arr[j - 1] > arr[j], swap the elements. Step 7: Continue until all elements are sorted. Step 8: Display the sorted array. Step 9: Stop the program. EX NO : 04 DATE : BUBBLE SORT 1 2 PROGRAM: //BSort.java import java.util.Scanner; public class BSort { public static void bubbleSort(int[] arr,int n){ for (int i = 0; i < n - 1; i++) { for(int j=1; j < (n - i); j++){ if(arr[j - 1] > arr[j]){ int temp = arr[j - 1]; arr[j - 1] = arr[j]; arr[j] = temp; } } } } public static void main(String a[]) { Scanner sc=new Scanner(System.in); System.out.print("Enter the number of elements you want to store: "); int n=sc.nextInt(); int[] array = new int[n]; System.out.println("Enter the elements of the array: "); for(int i=0; i<n; i++) { array[i]=sc.nextInt(); } System.out.println("Before Bubble Sort"); for(int i=0; i<n; i++) { System.out.print(array[i]+" "); } 1 3 System.out.println(); bubbleSort(array,n); System.out.println("After Bubble Sort"); for(int i=0; i<n; i++) { System.out.print(array[i]+" "); } } } 1 4 OUTPUT: RESULT: Thus the given program is Executed Successfully and Output is verified. 1 5 AIM: To write a Java program to search an element using Linear Search. ALGORITHM: Step 1: Start the program. Step 2: Read the number of elements n. Step 3: Read array elements. Step 4: Read the element to be searched key. Step 5: Compare key with each element in the array. Step 6: If element matches, return its position. Step 7: If the loop ends without finding the element, return - 1. Step 8: Display the result. Step 9: Stop the program. EX NO : 05 DATE : LINEAR SEARCH 1 6 PROGRAM: //LinearSearch.java import java.util.Scanner; public class LinearSearch { public static int linearSearch(int[] arr, int key,int n) { for(int i=0;i<n;i++) { if(arr[i] == key) { return i; } } return - 1; } public static void main(String a[]) { int n; Scanner sc=new Scanner(System.in); System.out.print("Enter the number of elements you want to store: "); n=sc.nextInt(); int[] array = new int[n]; System.out.println("Enter the elements of the array: "); for(int i=0; i<n; i++) { array[i]=sc.nextInt(); } System.out.print("Enter the element to search: "); int key =sc.nextInt(); int index= linearSearch(array, key,n); 1 7 if (index>=0) System.out.println(key+" is found at location: "+(index+1)); else System.out.println(key+" is not found "); } }