Search code examples
c++sortingbubble-sort

How do I sort students ID, name, and marks of students while using for bubble sorting?


I got these going which only sort the student's mark on an ascending order, but I can't sort the name and the ID of the students by their marks.

#include <iostream>
using namespace std;

int main()
{
    int total;
    cin >> total; // How much data you want to input

    string ID[100];
    string name[100];
    int grade[total];

    for (int i = 0; i < total; i++) // Inputting the data
    {
        cin >> ID[i]; // Student ID
        cin >> name[i]; // student name
        cin >> grade[i]; // student marks
    }

    for (int i = 0; i < total - 1; i++) {
        for (int j = i + 1; j < total; j++) {
            if (grade[j] < grade[i]) {
                int temp = grade[j];
                grade[j] = grade[i];
                grade[i] = temp;
            }
        }
    }

    for (int i = 0; i < total; i++) {
        cout << ID[i] << " " << name[i] << " " << grade[i] << endl;
    }
    return 0;
}

Solution

  • As mentioned by my comment, another alternative is to not sort the arrays themselves, but to sort an index array.

    This allows you to keep the arrays separate, and without having to write n sets of "swap code" if you have n arrays to handle.

    Below is mostly your code, but with what has been mentioned being applied:

    #include <iostream>
    #include <string>
    
    int main() 
    {
        int total;
        std::cin >> total; 
        std::string ID[100];
        std::string name[100];
        int grade[100];
        int index[100];
    
        for (int i = 0; i < total && i < 100; i++) //Inputting the data
        {
            std::cin >> ID[i]; //Student ID
            std::cin >> name[i]; //student name
            std::cin >> grade[i]; //student marks 
            index[i] = i;  // must be set up (0, 1, 2, 3, ..., total-1)
        } 
    
        for (int i = 0; i < total-1; i++) 
        {
            for (int j = i + 1; j < total; j++) 
            {
                // If necessary, swap index[i] and index[j] if the
                // grades at those locations are out of order
                if (grade[index[j]] < grade[index[i]]) 
                {
                    int temp = index[j];
                    index[j] = index[i];
                    index[i] = temp;
                }
            }
        } 
    
        // Output the sorted data using the index array 
        for (int i = 0; i < total; i++) {
            std::cout << ID[index[i]] << " " << name[index[i]] << " " << grade[index[i]] << std::endl;
        }
    
        // For reference, output the final index array.
        std::cout << "\nIndex Array:\n";
        for (int i = 0; i < total; i++) {
            std::cout << index[i] << " ";
        }
    }
    

    Input:

    4 
    1 Joe 50
    2 Jack 40
    3 Mary 80
    4 Sam 75
    

    Output:

    2 Jack 40
    1 Joe 50
    4 Sam 75
    3 Mary 80
    
    Index Array:
    1 0 3 2