This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
/*This is a java program to find permutation of N numbers using Heap’s Algorithm. Heap’s algorithm is an algorithm used for generating all possible permutations of some given length.*/
importjava.util.Arrays;
importjava.util.Scanner;
publicclassHeapsPermutation
{
privatestaticvoidswap(int[]v,inti,intj)
{
intt=v[i];
v[i]=v[j];
v[j]=t;
}
publicvoidpermute(int[]v,intn)
{
if(n==1)
{
System.out.println(Arrays.toString(v));
}
else
{
for(inti=0;i<n;i++)
{
permute(v,n-1);
if(n%2==1)
{
swap(v,0,n-1);
}
else
{
swap(v,i,n-1);
}
}
}
}
publicstaticvoidmain(String[]args)
{
System.out.println("Enter the number of elements in a sequence: ");