1

I am supposed to receive n, and create an array of 1 to n, then print the subsets in alphabetic order, but the goal is to create them in an alphabetic order (no array sorting) , for example for n=3 the right order would be: {} {1} {1,2} {1,2,3} {1,3} {2} {2,3} {3}.

so my current code finds the subsets:

    for (int code = 0; code < pow(2, n); code++) {
        printf("{");
        counter = 0;
        for (int i = 0; i < n; i++) {
            if (code & (1 << i)) {
                counter++;
                if (counter == 1)
                    printf("%d", nums[i]);
                else
                    printf(", %d", nums[i]);
            }
        }
        puts("}");
    }

The problem is printing them in the required order, and I want to write a function that would output the subsets in that order, without any sorting taking place.

The other alternative I checked was a DFS/backtracking algorithm, but even then, they won't be printed in the exact required order.

So I was wondering if DFS is the right path to go (with modifications) or if there is another algorithm which would suit me better?

chqrlie
  • 131,814
  • 10
  • 121
  • 189
arman5592
  • 41
  • 8

2 Answers2

1

To achieve your goal, you can use a recursive function that performs these steps:

  1. start with an empty set and index i at 0.
  2. print the current set
  3. increment i to the next number
  4. if i is above the maximum number, stop
  5. add number i to the set and recurse at step 2
  6. remove number i and iterate at step 3

Here is a simple implementation:

#include <stdio.h>
#include <stdlib.h>

static void printset(const int set[], int n) {
    printf("{");
    for (int i = 0; i < n; i++) {
        if (i > 0)
            printf(",");
        printf("%d", set[i]);
    }
    puts("}");
}

static void printsets(int set[], int count, int i, int n) {
    printset(set, count);
    while (++i <= n) {
        set[count] = i;
        printsets(set, count + 1, i, n);
    }
}

int main(int argc, char *argv[]) {
    int n;
    if (argc > 1) {
        n = strtol(argv[1], NULL, 0);
    } else {
        printf("Enter n: ");
        if (scanf("%d", &n) != 1)
            return 1;
    }
    int set[n];
    printsets(set, 0, 0, n);
    return 0;
}

Here is a variant that prints all 67108864 subsets of the alphabet in alphabetical order in about 10 seconds:

#include <stdio.h>

static void printsets(char set[], int len, char c) {
    printf("%.*s\n", len, set);
    while (c <= 'Z') {
        set[len] = c;
        printsets(set, len + 1, ++c);
    }
}

int main() {
    char set[26];
    printsets(set, 0, 'A');
    return 0;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
0

If you want to print an array in alphabetical order, and you cannot Sort the array, then you have to insert the elements in the array in the desired order. Make a new array with the subsets where you insert them in the order and pass it to the print function, I would recommend you to create a compare function for the subsets.

  • the array is sorted (1 to n) but the problem is that I want to print all possible subsets in an alphabetical order – arman5592 Dec 07 '19 at 18:44
  • What I mean, is that you can create a new array, in this new array you have to insert all the subsets of the given set. The trick is that you have to modify the array each time that you insert an element to keep the order , and then just print the array from index 0 to I index n-1. –  Dec 07 '19 at 18:53