Home:ALL Converter>c program Bubble Sort for strings

c program Bubble Sort for strings

Ask Time:2017-04-21T06:39:58         Author:sarah campolt

Json Formatter

I am writing a program that takes an array of pointers to strings and I am writing a function to put them in order. I think I have the ground work of bubble sort, but all the items in the list are just switching one position instead of being sorted.

Here is main

char *bubble[5] = { "cats", "dogs", "bananas", "apples", "sugar" };
int i = 0;
for (i = 0; i < 5; i++)
{
    printf("%s ", bubble[i]);
}
printf("\n");
bubble_sort(bubble, 5);
i = 0;
for (i = 0; i < 5; i++)
{
    printf("%s ", bubble[i]);
}
printf("\n");

and here is my function

void bubble_sort(int *list[], int size)
/*
This sorts a list of strings
*/
{
    int u = 0, c = 0, passes = 0;
    char *temp = '/0';
    u = size - 1;

    while (u > 0)
    {
        c = 1;
        while (c < u)
        {
            if (strcmp(list[c], list[c - 1]))
            {
                temp = list[c];
                list[c] = list[c-1];
                list[c - 1] = temp;
                c++;
            }
            u--;
        }
    }

}

I guess my problem is getting the pointers to change the contents of the bubble array so that I can print in main

Author:sarah campolt,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/43531068/c-program-bubble-sort-for-strings
yy