0% found this document useful (0 votes)
20 views9 pages

DAA Lab Programmes

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views9 pages

DAA Lab Programmes

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

2.

Implement next permutation, which rearranges numbers into the lexicographically next greater
permutation of numbers.If such an arrangement is not possible, it must rearrange it as the lowest
possible order (i.e., sorted in ascending order).The replacement must be in place and use only
constant extra memory.

Algorithm:
1. Find the longest non-increasing suffix and find the pivot.
2. If the suffix is the whole array, then there is no higher order permutation for the data.
3. Find the rightmost successor to the pivot.
4. Swap the successor and the pivot.
5. Reverse the suffix.

import java.util.Arrays;
public class NextPermutation {
public static int[] swap(int data[], int left, int right)
{
int temp = data[left];
data[left] = data[right];
data[right] = temp;
return data;
}

public static int[] reverse(int data[], int left, int right)


{

while (left < right) {


int temp = data[left];
data[left++] = data[right];
data[right--] = temp;
}
return data;
}

public static boolean findNextPermutation(int data[])


{

if (data.length <= 1)
return false;

int last = data.length - 2;


while (last >= 0) {
if (data[last] < data[last + 1]) {
break;
}
last--;
}
if (last < 0)
return false;
int nextGreater = data.length - 1;
for (int i = data.length - 1; i > last; i--) {
if (data[i] > data[last]) {
nextGreater = i;
break;
}
}
data = swap(data, nextGreater, last);
data = reverse(data, last + 1, data.length - 1);
return true;
}
public static void main(String args[])
{
int data[] = { 1, 2, 3 };
if (!findNextPermutation(data))
System.out.println("There is no higher"
+ " order permutation "
+ "for the given data.");
else {
System.out.println(Arrays.toString(data));
}
}
}
3. 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 using
StringTokenizer class considering the delimiter character as “/”.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Customer c = new Customer();

System.out.println("Enter name");
String name = br.readLine();

System.out.println("Enter DOB in (DD/MM/YYYY) format only");


String dob = br.readLine();

c.Read_Customer(name, dob);
c.Display_Customer();
}
}
class Customer
{
private String name;
private String dob;

void Read_Customer(String n, String d)


{
name = n;
dob = d;
}
void Display_Customer()
{
StringTokenizer st = new StringTokenizer(dob,"/");
System.out.println("Customer details are");
System.out.println("Name," + "\t" + "DD, MM, YYYY");
System.out.print(name+",\t");

while(st.hasMoreTokens())
{
System.out.print(st.nextToken() + ", ");
}
}
}
Sample Output:
Enter name
Kaveri Enter
DOB in (DD/MM/YYYY) format only
20/02/2000 C
customer details are
Name, DD, MM, YYYY
Kaveri, 20, 02, 2000

5. Write a java program to implement the following sorting techniques by using Divide and Conquer
Method: a) Insertion Sort b) Selection sort

a) Insertion Sort
class InsertionSort {
void sort(int arr[])
{
int n = arr.length;
for (int i = 1; i < n; ++i) {
int key = arr[i];
int j = i - 1;

while (j >= 0 && arr[j] > key) {


arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}

static void printArray(int arr[])


{
int n = arr.length;
for (int i = 0; i < n; ++i)
System.out.print(arr[i] + " ");

System.out.println();
}

public static void main(String args[])


{
int arr[] = { 12, 11, 13, 5, 6 };
InsertionSort ob = new InsertionSort();
ob.sort(arr);

printArray(arr);
}
}

Output:

5 6 11 12 13

b) Selection Sort

class SelectionSort
{
void sort(int arr[])
{
int n = arr.length;

for (int i = 0; i < n-1; i++)


{

int min_idx = i;
for (int j = i+1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
int temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
}
void printArray(int arr[])
{
int n = arr.length;
for (int i=0; i<n; ++i)
System.out.print(arr[i]+" ");
System.out.println();
}

public static void main(String args[])


{
SelectionSort ob = new SelectionSort();
int arr[] = {64,25,12,22,11};
ob.sort(arr);
System.out.println("Sorted array");
ob.printArray(arr);
}
}

Output:
Sorted array:
11 12 22 25 64

6. Write a java program for an array of jobs where every job has a deadline and associated profit if
the job is finished before the deadline. It is also given that every job takes single unit of time, so
the minimum possible deadline for any job is 1. How to maximize total profit if only one job can be
scheduled at a time

import java.util.*;

public class job

public static void main(String args[])

Scanner sc=new Scanner(System.in);

System.out.println("Enter the number of Jobs");

int n=sc.nextInt();

String a[]=new String[n];

int b[]=new int[n];

int c[]=new int[n];

for(int i=0;i<n;i++)

System.out.println("Enter the Jobs");

a[i]=sc.next();

System.out.println("Enter the Profit");

b[i]=sc.nextInt();

System.out.println("Enter the DeadLine");

c[i]=sc.nextInt();

System.out.println("--Arranged Order--");
System.out.print("Jobs: ");

for(int i=0;i<n;i++)

System.out.print(a[i]+" ");

System.out.println();

System.out.print("Profit: ");

for(int i=0;i<n;i++)

System.out.print(b[i]+" ");

System.out.println();

System.out.print("DeadLine:");

for(int i=0;i<n;i++)

System.out.print(c[i]+" ");

for(int i=0;i<n-1;i++)

for(int j=i+1;j<n;j++)

if(b[i]<b[j])

int temp=b[i];

b[i]=b[j];

b[j]=temp;

temp=c[i];

c[i]=c[j];

c[j]=temp;

String temp1=a[i];

a[i]=a[j];
a[j]=temp1;

System.out.println();

System.out.println("--Sorted Order--");

System.out.print("Jobs: ");

for(int i=0;i<n;i++)

System.out.print(a[i]+" ");

System.out.println();

System.out.print("Profit: ");

for(int i=0;i<n;i++)

System.out.print(b[i]+" ");

System.out.println();

System.out.print("DeadLine:");

for(int i=0;i<n;i++)

System.out.print(c[i]+" ");

System.out.println();

int max=c[0];

for(int i=0;i<n;i++)

if(c[i]>max)

max=c[i];

}
}

String x[]=new String[max];

int xx[]=new int[max];

int profit=0;

for(int i=0;i<n;i++)

int pp=c[i];

pp=pp-1;

if(x[pp]==null )

x[pp]=a[i];

profit+=b[i];

else

while(pp!=-1)

if(x[pp]==null)

x[pp]=a[i];

profit+=b[i];

break;

pp=pp-1;

for(int i=0;i<max;i++)

System.out.print("-->"+x[i]);

}
System.out.println();

System.out.print("Profit Earned"+profit);

Output:

Enter the number of Jobs

Enter the Jobs1

Enter the Profit

Enter the DeadLine

Enter the Jobs

Enter the Profit

Enter the DeadLine

--Arranged Order--

Jobs: 1 2

Profit: 2 1 DeadLine:2 1 --Sorted Order--

Jobs: 1 2

Profit: 2 1

DeadLine:2 1

-->2-->1

Profit Earned3

You might also like