cfunctionbubble-sort

Writing Bubble Sorting Function


I'm trying to write a simple bubble sort function to sort some random data. Whenever executed though it simply prints the unsorted list as it was written initially. Where did i go wrong??? Code:

#include <cs50.h>
#include <stdio.h>

#define NUM_CITIES 10

typedef struct
{
    string city;
    int temp;
}
avg_temp;

avg_temp temps[NUM_CITIES] = {
    {"Austin", 97}, {"Boston", 82"}, {"Chicago", 85},
    {"Denver", 90}, {"Las Vegas", 105}, 
    {"Los Angeles", 82}, {"Miami", 97},
    {"New York", 85}, {"Phoenix", 107},
    {"San Fransisco", 66}
};

void sort_cities(void);

int main(void)
{
    sort_cities();

    printf("\nAverage July Temperatures by City\n\n");

    for (int i = 0; i < NUM_CITIES; i++)
    {
        printf("%s: %i\n", temps[i].city, temps[i].temp);
    }
}

void sort_cities(void)
{
    for (int i = 0; i < NUM_CITIES - 1; i++)
    {
        for (int j = 0; j < NUM_CITIES - i - 1; i++)
        {
            if (temps[j].temp < temps[j + 1].temp)
            {
                avg_temp initial = temps[j];
                temps[j] = temps[j + 1];
                temps[j + 1] = initial;
            }
        }
    }
}

I've tried thinking through things logically and step by step it makes sense to me, i do believe in the function it gets sorted but maybe not in my main somehow?


Solution

  • I had to change a couple of things so it would compile on godbolt, but mostly please see the changes I made to sort_cities() function. That was the pain point.

    Note especially addition of variable need_sort.

    LOGIC: As long as a swap is needed, the array still needs a sort. We always set need_sort = 0 (false) at the top of each loop. And if a swap is needed/performed, need_sort is set to 1 (true) again to flag that sorting must continue. On the last pass through the array, no swap is made because all the array components are in order, need_sort remains false, and the loop finally breaks.

    In simple terms, this logic wasn't happening/executing in the original code. Hence sorting did not occur.

    Also removed outer for() loop as while() improves and clarifies usage of variable need_sort.

    Runnable code is provided here

    #include <stdio.h>
    
    #define NUM_CITIES 10
    
    typedef struct
    {
        char city[20];
        int temp;
    }avg_temp;
    
    avg_temp temps[NUM_CITIES] = {
    "Austin",97,
    "Boston",82,
    "Chicago",85,
    "Denver",90,
    "Las Vegas",105,
    "Los Angeles",82,
    "Miami",97,
    "New York",85,
    "Phoenix",107,
    "San Francisco",66
    };
    
    
    void sort_cities(void);
    
    int main()
    {
        
        sort_cities();
    
        printf("\nAverage July Temperatures by City\n");
    
        for (int i = 0; i < NUM_CITIES; i++)
        {
            printf("%s: %i\n", temps[i].city, temps[i].temp);
        }
        return 0;
    }
    
    void sort_cities(void)
    {
    int j, need_sort = 1;
    
        while(need_sort)
        {
            need_sort = 0;
    
            for (j = 0; j < NUM_CITIES-1; j++)
            {
                if (temps[j].temp > temps[j + 1].temp)
                {
                    avg_temp initial = temps[j];
                    temps[j] = temps[j + 1];
                    temps[j + 1] = initial;
                    need_sort=1;
                }
            }
        }
    }
    

    Output:

        
        Average July Temperatures by City
        San Francisco: 66
        Boston: 82
        Los Angeles: 82
        Chicago: 85
        New York: 85
        Denver: 90
        Austin: 97
        Miami: 97
        Las Vegas: 105
        Phoenix: 107