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.
#include<stdio.h>
#include<math.h>
int i, j;
int sequence[] = { 2, 3, 5, 6, 7 };
void lexicographicOrder(int N) {
int binary[(int) pow(2, N)];
for (i = 0; i < pow(2, N); i++) {
int b = 1;
binary[i] = 0;
int num = i;
while (num > 0) {
binary[i] += (num % 2) * b;
num /= 2;
b = b * 10;
}
}
printf("\nThe permutations are: ");
for (i = 0; i < pow(2, N); i++) {
printf("{ ");
for (j = 0; j < N; j++) {
if (binary[i] % 10 == 1)
printf("%d ", sequence[j]);
binary[i] /= 10;
}
printf("}\n");
}
}
int main(int argc, char **argv) {
printf("The elements in the set : ");
for (i = 0; i < 5; i++)
printf("%d ", sequence[i]);
for (i = 1; i < 5; i++) {
int j = i;
int temp = sequence[i];
while (j > 0 && temp < sequence[j - 1]) {
sequence[j] = sequence[j - 1];
j = j - 1;
}
sequence[j] = temp;
}
lexicographicOrder(5);
}
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.
Related Posts:
- Watch Advanced C Programming Videos
- Check C Books
- Practice BCA MCQs
- Apply for Computer Science Internship
- Practice Computer Science MCQs