DAA Lab Programmes
DAA Lab Programmes
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;
}
if (data.length <= 1)
return false;
System.out.println("Enter name");
String name = br.readLine();
c.Read_Customer(name, dob);
c.Display_Customer();
}
}
class Customer
{
private String name;
private String dob;
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;
System.out.println();
}
printArray(arr);
}
}
Output:
5 6 11 12 13
b) Selection Sort
class SelectionSort
{
void sort(int arr[])
{
int n = arr.length;
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();
}
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.*;
int n=sc.nextInt();
for(int i=0;i<n;i++)
a[i]=sc.next();
b[i]=sc.nextInt();
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];
}
}
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:
--Arranged Order--
Jobs: 1 2
Jobs: 1 2
Profit: 2 1
DeadLine:2 1
-->2-->1
Profit Earned3