Design and Analysis of Algorithms Lab Manual
Design and Analysis of Algorithms Lab Manual
Program-1:
(1)Write a java program to implement Merge sort algorithm for sorting a list of integers in
ascending order.
Program code:
import java.util.Arrays;
public static void mergeSort(int[] array, int left, int right) {//
public static void merge(int[] array, int left, int mid, int right) {
int i = 0, j = 0;
int k = left;
array[k] = L[i];
i++;
} else {
array[k] = R[j];
j++;
k++;
array[k] = L[i];
i++;
k++;
array[k] = R[j];
j++;
k++;
Program code:
import java.util.ArrayList;
import java.util.List;
public class SubsetSum {
public static void main(String[] args) {
int[] nums = {10, 7, 5, 18, 12, 20, 15};
int targetSum = 35;
System.out.println("Original Array: ");
for (int num : nums) {
System.out.print(num + " ");
}
System.out.println("\n\nSubsets with sum " + targetSum + ": ");
List<List<Integer>> subsets = findSubsets(nums, targetSum);
for (List<Integer> subset : subsets) {
System.out.println(subset);
}
}
public static List<List<Integer>> findSubsets(int[] nums, int targetSum) {
List<List<Integer>> result = new ArrayList<>();
backtrack(result, new ArrayList<>(), nums, targetSum, 0);
return result;
}
private static void backtrack(List<List<Integer>> result, List<Integer> tempList, int[] nums,
int remain, int start) {
if (remain < 0) return;
else if (remain == 0) result.add(new ArrayList<>(tempList));
else {
for (int i = start; i < nums.length; i++) {
tempList.add(nums[i]);
backtrack(result, tempList, nums, remain - nums[i], i + 1);
tempList.remove(tempList.size() - 1);
}
}
}
}
Output:
Original Array:
10 7 5 18 12 20 15
Subsets with sum 35:
[10, 7, 18]
[10, 5, 20]
[5, 18, 12]
[20, 15]
Program(3):
(3)Write a Java program to implement the Stack using arrays. Write Push(), Pop(), and
Display() methods to demonstrate its working.
Program Code:
Program(4):
(4) Write a Java class called Customer to store their name and date_of_birth. The date_of_birth
format should be dd/mm/yyyy. Write methods to read customer data as and display as
usingStringTokenizer classconsideringthedelimiter characteras.
Program Code:
import java.util.Scanner;
import java.util.StringTokenizer;
class Customer
{
private String customerName,date;
public Customer(String customerName, String date)
{
super();
this.customerName = customerName;
this.date = date;
}
@Override
public String toString()
{
String returnValue = customerName+",";
StringTokenizer tokenizer = new StringTokenizer(date,"/");
System.out.println("The Customer details are ");
while(tokenizer.hasMoreTokens())
{
returnValue = returnValue+tokenizer.nextToken();
if(tokenizer.hasMoreElements())
{
returnValue = returnValue+",";
}
}
return returnValue;
}
}
public class CustomerDetails
{
public static void main(String[] args)
{
String customerName;
String date;
Scanner scanner = new Scanner(System.in);
System.out.println("Enter customer name");
customerName = scanner.next();
System.out.println("Enter Date (dd/mm/yyy)");
date = scanner.next();
Customer customer = new Customer(customerName,date);
System.out.println(customer.toString());
}
}
Out put:
Enter customer name
mani
Enter Date (dd/mm/yyy)
03/03/2001
The Customer details are
mani,03,03,2001
Program(5)
(5) Write a Java program to read two integers a and b. Compute a/b and print, when b is not zero.
Raise an exception when b is equal to zero.
Program Code:
import java.util.*;
public class Division
{
public static void main(String[] args)
{
int a,b,quotient;
Scanner s = new Scanner(System.in);
System.out.println("Enter Numerator:" );
a = s.nextInt();
System.out.println("Enter Denominator:" );
b = s.nextInt();
try
{
quotient=a/b;
System.out.println("Quotient=" + quotient);
}
catch(ArithmeticException ae)
{
System.out.println(ae);
}
}
}
Output: output:
Enter Numerator: Enter Numerator:
100 50
Enter Denominator: Enter Denominator:
50 0
Quotient=2 java.lang.ArithmeticException: / by zero
Program(6):
(6) Design and implement in Java to find a sub set of a given set S = {Sl,S2,.....,Sn} of n positive
integers whose SUM is equal to a given positive integer d. For example, if S ={1, 2, 5, 6, 8} and d= 9,
there are two solutions {1,2,6}and {1,8}. Display a suitable message, if the given problem instance
doesn't have a solution.
Program Code:
import java.util.ArrayList;
import java.util.List;
int d = 9;
if (subsets.isEmpty()) {
} else {
System.out.println(subset);
}
}
return result;
private static void backtrack(List<List<Integer>> result, List<Integer> tempList, int[] set, int remain,
int start) {
else {
tempList.add(set[i]);
tempList.remove(tempList.size() - 1);
Output:
Original Set:
12568
[1, 2, 6]
[1, 8]
Program(7):
(7)Write a java program to implement binary search algorithm with programmer inputs.
Program code:
import java.util.Scanner;
public class BinarySearch {
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;
if (arr[mid] < target)
left = mid + 1;
else
right = mid - 1;
}
return -1;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the size of the array: ");
int size = scanner.nextInt();
int[] arr = new int[size];
System.out.println("Enter the elements of the array in sorted order:");
for (int i = 0; i < size; i++) {
arr[i] = scanner.nextInt();
}
System.out.print("Enter the number to search: ");
int target = scanner.nextInt();
int index = binarySearch(arr, target);
if (index != -1) {
System.out.println("Element found at index: " + index);
} else {
System.out.println("Element not found in the array.");
}
scanner.close();
}
}
Output:
Enter the size of the array: 5
Enter the elements of the array in sorted order:
10
20
30
40
50
Enter the number to search: 40
Element found at index :3
Program(8):
(8)Write a java program to find third largest element in an array?With programmer inputs.
Program Code:
import java.util.Scanner;
Output:
Enter the size of the array: 5
Enter the elements of the array:
23
34
56
45
67
The third largest number in the array is: 45
Program(9):
(9)Write a java program to reverse of a string with programmer inputs?
Program code:
import java.util.Scanner;
scanner.close();
}
}
Output:
Enter a string: 12345
Reversed string: 54321
Program(10)
(10)Write a java program to implement Fibonacci series in java using recursion.
Program code:
import java.util.Scanner;
System.out.println("Fibonacci series:");
for (int i = 0; i < numTerms; i++) {
System.out.print(fibonacci(i) + " ");
}
scanner.close();
}
}
Output:
Enter the number of terms in the Fibonacci series: 10
Fibonacci series:
0 1 1 2 3 5 8 13 21 34