Power set generated by bits

前端 未结 5 1463
青春惊慌失措
青春惊慌失措 2021-02-04 06:29

I have this code which generates power set of an array of size 4 (number is just example, less combinations to write...).

#define ARRAY_SIZE 4


unsigned int i,          


        
5条回答
  •  我在风中等你
    2021-02-04 06:43

    Use a buffer array and then sort it using qsort ?

    I used a i_max*5 elements array : 4 unsigned int for the values (0 means empty, 1 to 4 otherwise) and a final unsigned int for the subset length. Then, you just have to roll with your custom comparator. If you want to a more streamlined algorithm, take a look at insertion sort : http://en.wikipedia.org/wiki/Insertion_sort

    Output:

    1,
    2,
    1,2,
    3,
    1,3,
    2,3,
    1,2,3,
    4,
    1,4,
    2,4,
    1,2,4,
    3,4,
    1,3,4,
    2,3,4,
    1,2,3,4,
    
    Sorting
    1,
    2,
    3,
    4,
    1,2,
    1,3,
    1,4,
    2,3,
    2,4,
    3,4,
    1,2,3,
    1,2,4,
    1,3,4,
    2,3,4,
    1,2,3,4,
    

    Code:

    #include 
    #include 
    #include 
    
    int comp (const void * elem1, const void * elem2) {
        unsigned int *f = (unsigned int*)elem1;
        unsigned int *s = (unsigned int*)elem2;
        unsigned int i;
    
    
        //printf("%d,%d,%d,%d,%d\n", f[0],f[1],f[2],f[3],f[4]);
        //printf("%d,%d,%d,%d,%d\n", s[0],s[1],s[2],s[3],s[4]);
        printf("\n");
    
        // Size comparison     
        if( f[4] > s[4] ) return 1;
        if( f[4] < s[4] ) return -1;
    
        // Value comparison
        for ( i = 0; i < 4; i++)
        {
            if (f[i] > s[i]) return  1;
            if (f[i] < s[i]) return -1;
        }
    
        return 0;
    }
    
    int main(int argc, char *argv[])
    {
        unsigned int i, j, bits, i_max = (1U << 4);
        unsigned int *array;
        unsigned int index;
    
        array = calloc( 5*i_max, sizeof(unsigned int));
    
        // Non-sorted array
        for (i = 0; i < i_max ; ++i) {
    
            // Zero init for array
            memset(array + i*5, 0, 5*sizeof(*array));
            index = 0;
    
            for (bits = i, j = 0; bits; bits >>= 1, ++j) {
    
            if (bits & 1)
                {
                    // Storage
                    array[i*5 + index]   = j+1;
    
                    index = (index + 1) % 4; // avoid buffer overflows
                    array[i*5 + 4]   = array[i*5 + 4] + 1 ; // increment subset length
    
                    //printf("%d,", array[i*5 + index - 1]);
                }
            }
            printf("\n");
        }
    
        // Print original array, without the zeros
        for (i =0; i < i_max; i++)
        {
            for(j = 0; j < 4 ; j++)
            {
                if( array[i*5 + j] )
                    printf("%d,", array[i*5 + j]);
            }
            printf("\n");
        }
    
    
        printf ("Sorting\n");
    
        qsort (array, i_max, 5*sizeof(unsigned int), comp);
    
    
        // Print the sorted array, without the zeros
        for (i =0; i < i_max; i++)
        {
            for(j = 0; j < 4 ; j++)
            {
                if( array[i*5 + j] )
                    printf("%d,", array[i*5 + j]);
            }
            printf("\n");
        }
        free(array);
    
        return 0;
    }
    

提交回复
热议问题