简体   繁体   中英

C++ Struct Bubble Sorting

I have to bubble sort a C++ struct only with iostream and fstream, something just seems to be going wrong though (see input and output below).

void sortCarsByPrice(Cars carsArray[])
{
    int index1;
    int index2;
    int index3;
    int swap;
    int yearTemp;
    char makeTemp;
    char modelTemp;
    float priceTemp;
    int availTemp;
    do
    {
        swap = 0;
        for (index1 = 0; index1 < 5; index1++)
        {
            if (carsArray[index1].price > carsArray[index1 + 1].price)
            {
                yearTemp = carsArray[index1].year;
                carsArray[index1].year = carsArray[index1 + 1].year;
                carsArray[index1 + 1].year = yearTemp;

                for (index2 = 0; carsArray[index1].make[index2] != '\0' && carsArray[index1 + 1].make[index2] != '\0'; index2++)
                {
                    makeTemp = carsArray[index1].make[index2];
                    carsArray[index1].make[index2] = carsArray[index1 + 1].make[index2];
                    carsArray[index1 + 1].make[index2] = makeTemp;
                }

                for (index3 = 0; carsArray[index1].model[index3] != '\0' && carsArray[index1 + 1].model[index3] != '\0'; index3++)
                {
                    modelTemp = carsArray[index1].model[index3];
                    carsArray[index1].model[index3] = carsArray[index1 + 1].model[index3];
                    carsArray[index1 + 1].model[index3] = modelTemp;
                }

                priceTemp = carsArray[index1].price;
                carsArray[index1].price = carsArray[index1 + 1].price;
                carsArray[index1 + 1].price = priceTemp;

                availTemp = carsArray[index1].available;
                carsArray[index1].available = carsArray[index1 + 1].available;
                carsArray[index1 + 1].available = availTemp;

                swap += 1;
            }
        }
    }
    while (swap != 0);
    cout << "SORTED!" << endl;
    return; 

}

input:

    0 2014 Toyota Tacoma $115.12 0
    1 2015 Ford Fusion $90.89 1
    2 2009 Dodge Neon $45.25 0
    3 2015 Ford F150 $112.83 1
    4 2016 Subaru Outback $71.27 1

output:(after sorting)

0 999984304 �yota Ծ0#on $-1.84172e+33 32566
1 2009 Dodg Neonma $45.25 0
2 2016 Subae Outb $71.27 1
3 2015 Ford Fusi $90.89 1
4 2015 Fordru F150U�� $112.83 1

If there are 5 elements in your array, and you use index1 + 1 as an index inside your loop, then you only want index1 to go up to 4. As written, you are allowing index1 to go up to 5, which means index1 + 1 is 6, which is presumably off the end of the array.

Better yet, you might want to add an additional argument to your function that is the number of elements in the array, rather than having a mysterious (and incorrect) 5 in the middle of your sorting function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM