Search code examples
carrayssortingcharbubble-sort

Bubble Sort leaves array empty


I'm trying to sort an array of characters in C. My bubble sort algorithm is a standard one but when I try to print the arrays after using the sort method they appear to be empty.

Is this a problem with my pointer usage?

#include <stdio.h>
#include <string.h>

char Gstr1[256];
char Gstr2[256];

void load();
void printArrays();
void sortArray(char *array);

main()
{

    load();
    printArrays();
    sortArray(&Gstr1[0]);
    sortArray(&Gstr2[0]);
    printf("\n\n");
    printArrays();
}

void load()
{
    memcpy (Gstr1,"Sed aliquam neque fermentum leo semper sagittis. Curabitur imperdiet, libero vulputate laoreet tristique, magna justo posuere, quis rutrum ligula tortor vitae eros. Phasellus sed dignissim elit, nec dictum ligula. Vivamus ornare ultrices odio eget dictum.",255);

    memcpy (Gstr2,"Lorem ipsum dolor si amet, consectetur adipiscing elit. Aenean dapibus libero a convallis sodales. Mauris placerat nisl leo, vitae tincidunt turpis tristique in. Pellentesque vehicula nisl vitae efficitur ornare. Nunc imperdiet  sem, in aliquam rhoncus at.",255);
}

void printArrays()
{
    printf(Gstr1);
    printf("\n\n");
    printf(Gstr2);
}

void sortArray(char *array)
{
     int i,j;
     char temp;

     for(i=0;i<(256-1);i++)
        for(j=0;j<(256-i-1);j++)
        {
            if((int)*(array+j)>(int)*(array+(j+1)))
            {
                temp=*(array+j);
                *(array+j)=*(array+(j+1));
                *(array+(j+1))=temp;
            }
        }
}

Solution

  • This is because of the string length. The length of first string is 256 and second is 257, and you're not taking care of '\0' character. Hence, it is advisable to use memcpy_s() instead of memcpy() and also, use strlen() instead of hardcoding the array size in the for loops. However, with minor correction of the for loop limit, the following code produces the output.

    Code:

    #include <stdio.h>
    #include <string.h>
    
    char Gstr1[256];
    char Gstr2[256];
    
    void load();
    void printArrays();
    void sortArray(char *array);
    
    main()
    {
    
        load();
        printArrays();
        sortArray(&Gstr1[0]);
        sortArray(&Gstr2[0]);
        printf("\n\n");
        printArrays();
    }
    
    void load()
    {
        memcpy (Gstr1,"Sed aliquam neque fermentum leo semper sagittis. Curabitur imperdiet, libero vulputate laoreet tristique, magna justo posuere, quis rutrum ligula tortor vitae eros. Phasellus sed dignissim elit, nec dictum ligula. Vivamus ornare ultrices odio eget dictum.",255);
    
        memcpy (Gstr2,"Lorem ipsum dolor si amet, consectetur adipiscing elit. Aenean dapibus libero a convallis sodales. Mauris placerat nisl leo, vitae tincidunt turpis tristique in. Pellentesque vehicula nisl vitae efficitur ornare. Nunc imperdiet  sem, in aliquam rhoncus at.",255);
    }
    
    void printArrays()
    {
        printf(Gstr1);
        printf("\n\n");
        printf(Gstr2);
    }
    
    void sortArray(char *array)
    {
         int i,j;
         char temp;
    
         for(i=0;i<strlen(array);i++)
            for(j=0;j<(strlen(array)-i-1);j++)
            {
                if((int)*(array+j)>(int)*(array+(j+1)))
                {
                    temp=*(array+j);
                    *(array+j)=*(array+(j+1));
                    *(array+(j+1))=temp;
                }
            }
    }