C Program to Generate All Possible Subsets using Lexicographic Order

This is a C Program to generate all subsets of a given set in the Lexico Graphic Order. This program generates all permutation of n elements in lexicographic order, where n = 5.

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

  1. #include<stdio.h>
  2. #include<math.h>
  3. int i, j;
  4. int sequence[] = { 2, 3, 5, 6, 7 };
  5.  
  6. void lexicographicOrder(int N) {
  7.     int binary[(int) pow(2, N)];
  8.     for (i = 0; i < pow(2, N); i++) {
  9.         int b = 1;
  10.         binary[i] = 0;
  11.         int num = i;
  12.         while (num > 0) {
  13.             binary[i] += (num % 2) * b;
  14.             num /= 2;
  15.             b = b * 10;
  16.         }
  17.     }
  18.     printf("\nThe permutations are: ");
  19.     for (i = 0; i < pow(2, N); i++) {
  20.         printf("{ ");
  21.         for (j = 0; j < N; j++) {
  22.             if (binary[i] % 10 == 1)
  23.                 printf("%d ", sequence[j]);
  24.             binary[i] /= 10;
  25.         }
  26.         printf("}\n");
  27.     }
  28. }
  29.  
  30. int main(int argc, char **argv) {
  31.     printf("The elements in the set : ");
  32.     for (i = 0; i < 5; i++)
  33.         printf("%d ", sequence[i]);
  34.  
  35.     for (i = 1; i < 5; i++) {
  36.         int j = i;
  37.         int temp = sequence[i];
  38.         while (j > 0 && temp < sequence[j - 1]) {
  39.             sequence[j] = sequence[j - 1];
  40.             j = j - 1;
  41.         }
  42.         sequence[j] = temp;
  43.     }
  44.     lexicographicOrder(5);
  45. }

Output:

$ gcc LexicographicCode.c
$ ./a.out
 
The elements in the set : 2 3 5 6 7 
The permutations are: { }
{ 2 }
{ 3 }
{ 2 3 }
{ 5 }
{ 2 5 }
{ 3 5 }
{ 2 3 5 }
{ 6 }
{ 2 6 }
{ 3 6 }
{ 2 3 6 }
{ 5 6 }
{ 2 5 6 }
{ 3 5 6 }
{ 2 3 5 6 }
{ 7 }
{ 2 7 }
{ 3 7 }
{ 2 3 7 }
{ 5 7 }
{ 2 5 7 }
{ 3 5 7 }
{ 2 3 5 7 }
{ 6 7 }
{ 2 6 7 }
{ 3 6 7 }
{ 2 3 6 7 }
{ 5 6 7 }
{ 2 5 6 7 }
{ 3 5 6 7 }
{ 2 3 5 6 7 }

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

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

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.