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

Time:10-11

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

CodePudding user response:

IMHO your code is hardly C at all.

  • the only C things used are cout and cin
  • variable length array like int grade[total]; do not exist in C , only in C.

If you had written a student class which combines ID, name and grade in a way that they can't be handled separately, you would always swap a complete student with its name and with its ID. You could then even use a C algorithm like std::sort.

Here's how that might look like. But wou could still write your own bubble sort algorithm.

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
class Student
{
public:
    string ID{};
    string name{};
    int grade{};
};
int main() {

    int total;
    cin >> total; //How much data you want to input

    vector<Student> pupils(total);

    for (auto& student : pupils) //Inputting the data
    {
        cin >> student.ID; //Student ID
        cin >> student.name; //student name
        cin >> student.grade; //student marks
    }

    std::sort(pupils.begin(), pupils.end(),
        [](Student const& a, Student const& b) {return a.grade < b.grade; }
    );

    for (auto const& student : pupils)
    {
        cout << student.ID << " " << student.name << " " << student.grade << endl;
    }

    return 0;
}

Some remarks:

CodePudding user response:

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 
  • Related