Search code examples
csortingmultidimensional-arraybubble-sortfunction-definition

How do I turn a char bubblesort into a string bubblesort by string length


I have written a basic bubble sorting program that uses given input from a predetermined array or characters, but I want to change it so I can sort strings. Also I want to not sort it alphabetically, but sort them by string length.

my source.c

void sort(char array[], int size) {
    for (int i = 0; i < size - 1; i++) {
        for (int j = 0; j < size - i - 1; j++) {
            if (array[j] > array[j + 1]) {
                int temp = array[j];
                array[j] = array[j + 1];
                array[j + 1] = temp;
            }
        }
    }
}

void printArray(char array[], int size) {
    for (int i = 0; i < size; i++) {
        printf("%c ", array[i]); //%c -> %s
    }
}

int main(void) {

    char array[] = { 'F', 'A', 'D', 'B', 'C' };
        //char string[][] = { "hey", "Hello", "awesome" };


    int size = sizeof(array) / sizeof(array[0]);

    sort(array, size);
    printArray(array, size);

    return 0;
}

I had tried to make the char array[]; in the int main(void) a string array as char string[][]; and tried make any changes accordingly but I keep failing (building errors). I also don't understand how I should change it to sort by string length and not alphabet order


Solution

  • For starters pay attention to that your function implementation is inefficient because its nested loops will be executed many times for an already sorted array. That is the function does not take into account that a part of the array (or the whole array) is already sorted.

    If you want to sort an array by some condition then you should declare the function with one more parameter that will specify a comparison function similarly to the standard C function qsort.

    So supplying different functions you can sort the same array using different rules of comparisons of its element.

    You always should write more general functions.

    Here is a demonstration program that shows how such a function that implements the bubble sort method can be written for two-dimensional character arrays.

    #include <stdio.h>
    #include <string.h>
    
    enum { N = 6 };
    
    void bubble_sort( char s[][N], size_t n, int cmp( const void *, const void * ) )
    {
        for (size_t sorted = 1; !( n < 2 ); n = sorted)
        {
            for (size_t j = sorted = 1; j < n; j++)
            {
                if (cmp( &s[j], &s[j - 1] ) < 0)
                {
                    char tmp[N];
                    strcpy( tmp, s[j] );
                    strcpy( s[j], s[j - 1] );
                    strcpy( s[j - 1], tmp );
    
                    sorted = j;
                }
            }
        }
    }
    
    static int by_length( const void *a, const void *b )
    {
        char ( *s1 )[N] = ( char( * )[N] )a;
        char( *s2 )[N] = ( char( * )[N] )b;
    
        size_t n1 = strlen( *s1 );
        size_t n2 = strlen( *s2 );
    
        return ( n2 < n1 ) - ( n1 < n2 );
    }
    
    static int lexicographically( const void *a, const void *b )
    {
        char( *s1 )[N] = ( char( * )[N] )a;
        char( *s2 )[N] = ( char( * )[N] )b;
    
        return strcmp( *s1, *s2 );
    }
    
    int main( void )
    {
    
        char s[][N] =
        {
            "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten"
        };
    
        const size_t M = sizeof( s ) / sizeof( *s );
    
        for (size_t i = 0; i < M; i++)
        {
            printf( "\"%s\" ", s[i] );
        }
    
        putchar( '\n' );
    
        bubble_sort( s, M, by_length );
    
        for (size_t i = 0; i < M; i++)
        {
            printf( "\"%s\" ", s[i] );
        }
    
        putchar( '\n' );
    
        bubble_sort( s, M, lexicographically );
    
        for (size_t i = 0; i < M; i++)
        {
            printf( "\"%s\" ", s[i] );
        }
    
        putchar( '\n' );
    }
    

    The program output is

    "One" "Two" "Three" "Four" "Five" "Six" "Seven" "Eight" "Nine" "Ten"
    "One" "Two" "Six" "Ten" "Four" "Five" "Nine" "Three" "Seven" "Eight"
    "Eight" "Five" "Four" "Nine" "One" "Seven" "Six" "Ten" "Three" "Two"
    

    You can write even a more general function if your compiler supports variable length arrays.

    For example in this case the function declaration can look like

    void bubble_sort( size_t m, size_t n, char s[m][n], int cmp( const void *, const void * ) )