Java Program to Generate All Possible Subsets using Lexicographic Order

This is a java program to generate and print all the subsets of a given set as per lexicographical order, here we follow the numerical sequence. First generate all the subsets having only one element, then generate all the subsets having two elements and so on.

Here is the source code of the Java Program to Generate All Subsets of a Given Set in the Lexico-Graphic Order. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. //This is a java program to generate all permutation of n elements in lexicographic order
  2. import java.util.Random;
  3. import java.util.Scanner;
  4.  
  5. public class Lexicographic_Permutation 
  6. {
  7.     public static int[] lexicographicOrder(int N) 
  8.     {
  9.         int[] binary = new int[(int) Math.pow(2, N)];
  10.         for (int i = 0; i < Math.pow(2, N); i++) 
  11.         {
  12.             int b = 1;
  13.             binary[i] = 0;
  14.             int num = i;
  15.             while (num > 0) 
  16.             {
  17.                 binary[i] += (num % 2) * b;
  18.                 num /= 2;
  19.                 b = b * 10;
  20.             }
  21.         }
  22.         return binary;
  23.     }
  24.  
  25.     public static void main(String args[]) 
  26.     {
  27.         Random random = new Random();
  28.         Scanner sc = new Scanner(System.in);
  29.         System.out.println("Enter the number of elements in the set: ");
  30.         int N = sc.nextInt();
  31.         int[] sequence = new int[N];
  32.         for (int i = 0; i < N; i++)
  33.             sequence[i] = Math.abs(random.nextInt(100));
  34.  
  35.         System.out.println("The elements in the set : ");
  36.         for (int i = 0; i < N; i++)
  37.             System.out.print(sequence[i] + " ");
  38.  
  39.         for (int i = 1; i < N; i++) 
  40.         {
  41.             int j = i;
  42.             int temp = sequence[i];
  43.             while (j > 0 && temp < sequence[j - 1]) 
  44.             {
  45.                 sequence[j] = sequence[j - 1];
  46.                 j = j - 1;
  47.             }
  48.             sequence[j] = temp;
  49.         }
  50.  
  51.         int[] mask = new int[(int) Math.pow(2, N)];
  52.         mask = lexicographicOrder(N);
  53.  
  54.         System.out.println("\nThe permutations are: ");
  55.         for (int i = 0; i < Math.pow(2, N); i++) 
  56.         {
  57.             System.out.print("{ ");
  58.             for (int j = 0; j < N; j++) 
  59.             {
  60.                 if (mask[i] % 10 == 1)
  61.                     System.out.print(sequence[j] + " ");
  62.                 mask[i] /= 10;
  63.             }
  64.             System.out.println("}");
  65.         }
  66.         sc.close();
  67.     }
  68. }

Output:

$ javac Lexicographic_Permutation.java
$ java Lexicographic_Permutation
 
Enter the number of elements in the set: 
5
The elements in the set : 
19 3 37 7 22 
The permutations are: 
{ }
{ 3 }
{ 7 }
{ 19 }
{ 22 }
{ 37 }
{ 3 7 }
{ 3 19 }
{ 7 19 }
{ 3 22 }
{ 7 22 }
{ 19 22 }
{ 3 37 }
{ 7 37 }
{ 19 37 }
{ 22 37 }
{ 3 7 19 }
{ 3 7 22 }
{ 3 19 22 }
{ 7 19 22 }
{ 3 7 37 }
{ 3 19 37 }
{ 7 19 37 }
{ 3 22 37 }
{ 7 22 37 }
{ 19 22 37 }
{ 3 7 22 37 }
{ 3 19 22 37 }
{ 7 19 22 37 }
{ 3 7 19 37 }
{ 3 7 19 22 }
{ 3 7 19 22 37 }

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.