Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Modify the merge sort algorithm to sort a vector of employees by salary. (2)

ID: 3724304 • Letter: C

Question

C++ Modify the merge sort algorithm to sort a vector of employees by salary.

(2) Based on Problem P11.4 . Modify the merge sort algorithm to sort a vector of Employees by salary. The class Employee in this problem is assumed to be class Employee I public: Employee (string e_name, double e_salary) :name (e_name), salary (e_salary) f string get_name const return name; double get_salary) const return salary; h protected: string name; double salary; . Write a program that reads the list of people's records from a file data2.txt, creates a corresponding vector and displays the name and salary for each employee After that the program should sort vector and display the name and the salary of employees in the sorted vector (for details see IO sample below) . You can assume that data for each employee is stored in data2.txt as name I salary (one data record per line) and that the file has at least one line . Submit the solution as hmw.5.2.cpp . Sample input-output data2.txt _ Not...-- exe File Edit Format View Hel John Doe | 65000 Lisa Smithl75000 Alex Taylor |55000 Enplo yees. before sorting: ohn Doe, $65000 Lisa Smith. $75000 lex Taylor. $55000 Enployees. after sorting: lex Taylor. $55000 ohn Doe, $65000 Lisa Smith. $?5000 Press any key to continue . . .

Explanation / Answer

Here's the solution:

#include<bits/stdc++.h>
using namespace std;

class Employee
{
public:
    Employee(string e_name, double e_salary): name(e_name),salary(e_salary){}
    string get_name() const { return name;}
    double get_salary() const { return salary;}
protected:
    string name;
    double salary;

};

void merge(vector<Employee*> &e, int l, int m, int r)
{
    int i, j, k;
    int n1 = m - l + 1;
    int n2 = r - m;
    vector<Employee*> L1(n1);
    vector<Employee*> L2(n2);
    for (i = 0; i < n1; i++)
        L1[i] = e[l + i];
    for (j = 0; j < n2; j++)
        L2[j] = e[m + 1+ j];
    i = 0;
    j = 0;
    k = l;
    while (i < n1 && j < n2)
    {
        if (L1[i]->get_salary() <= L2[j]->get_salary())
        {
            e[k] = L1[i];
            i++;
        }
        else
        {
            e[k] = L2[j];
            j++;
        }
        k++;
    }
    while (i < n1)
    {
        e[k] = L1[i];
        i++;
        k++;
    }
    while (j < n2)
    {
        e[k] = L2[j];
        j++;
        k++;
    }
}

void merge_sort(vector<Employee*> &e, int l, int r)
{
    if (l < r)
    {
        int m = l+(r-l)/2;
        merge_sort(e, l, m);
        merge_sort(e, m+1, r);
        merge(e, l, m, r);
    }
}

void display(vector<Employee*> &e)
{
    for(int i=0;i<e.size();i++)
        cout<<e[i]->get_name()<<", "<<e[i]->get_salary()<<endl;
    cout<<endl;
}

int main()
{
    string name;
    double salary;
    vector<Employee*> employees;
    ifstream inp("data2.txt");
    string line;
    while(getline(inp,line))
    {
        int part = line.find("|");
        name = line.substr(0,part);
        salary = atof((line.substr(part+1)).c_str());
        Employee* temp = new Employee(name,salary);
        employees.push_back(temp);
    }
    cout<<"Employees, before sorting: ";
    display(employees);
    merge_sort(employees,0,employees.size()-1);
    cout<<"Employees, after sorting: ";
    display(employees);
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote