Vincent Del Vecchio
Vincent Del Vecchio

Reputation: 11

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;
            }
        }
}

Upvotes: 0

Views: 117

Answers (2)

B V Raman
B V Raman

Reputation: 212

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;
            }
        }
}

Upvotes: 1

DAXaholic
DAXaholic

Reputation: 35358

You sort the last character in the array as well resulting in a sorted character array with leading null terminator.
Therefore just adjust your loops to fix your issue as shown below.
Of course it would be better to pass the array size with a parameter but that's a different topic :)

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

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

Upvotes: 0

Related Questions