Below are my codes, small.txt file, and the output that needs to be printed. Thank you! #include #include #include #include using namespace std; class Employee { public: //Instantiate string name; int id; int age; string job; int year; //Constructors Employee() { name = ""; job = ""; age = year = id = 0; } Employee(string name, int id, int age, string job, int year) { this->name = name; this->id = id; this->age = age; this->job = job; this->year = year; } }; Employee data(string line) { string input; stringstream ss(line); string name; string job; int id; int age; int year; getline(ss, name, '|'); getline(ss, input, '|'); id = atoi(input.c_str());//convert the int to string getline(ss, input, '|'); age = atoi(input.c_str());//convert the int to string getline(ss, job, '|'); getline(ss, input, '\n'); year = atoi(input.c_str());//convert the int to string Employee emp(name, id, age, string(job), year); return emp; }; //Comparison Counter int ctr; void maxHeapify(Employee arr[], int size, int i) { int largest = i; int left = 2 * i + 1; int right = 2 * i + 2; if (left < size && arr[left].id > arr[largest].id) { largest = left; ctr++; } if (right < size && arr[right].id > arr[largest].id) { largest = right; ctr++; } if (largest != i) { swap(arr[i], arr[largest]); maxHeapify(arr, size, largest); } } void buildMaxHeap(Employee arr[], int size) { for (int i = size / 2 - 1; i >= 0; i--) { maxHeapify(arr, size, i); } } void printResult(Employee arr[], int size) { for (int m = 0; m < size; m++) { cout << arr[m].id << (m + 1 == size ? "\n" : " "); } } void heapSort(Employee arr[], int size) { //print before building max heap printResult(arr, size); buildMaxHeap(arr, size); //print after building max heap printResult(arr, size); for (int i = size - 1; i > 0; i--) { swap(arr[0], arr[i]); maxHeapify(arr, i, 0); printResult(arr, size); } } int main(int argc, char const* argv[]) { //Catches error conditions Check if filename included as parameter if (argc != 2) { cout << "You need to include a filename on the command line.\n"; return -1; } string filename = string(argv[1]);//Fetches the file name fstream file(filename, ios::in); //Catches error conditions Check if file exist if (!file) { cout << "Error opening file: " << filename << endl; return -1; } int size; string input;//store the inputs or the lines from the file file >> size; getline(file, input);//read the entire file line by line Employee* employees = new Employee[size]; for (int i = 0; i < size; ++i) { getline(file, input); employees[i] = data(input); } heapSort(employees, size); cout << "It took " << ctr << " comparisons to sort this list.\n"; file.close(); return 0; } small.txt file 10 Antharion Buxyho|10|31|Engineer|2020 Molly Nyxujijo|2|42|Custodian|2016 Grubbo Totexyca|9|47|Tester|2011 Thriff Cosapeka|7|24|Technician|2019 Grubbo Neferele|6|61|Director|1995 Borphee Rozuzy|5|23|Director|2019 Jeearr Heposiko|1|32|Engineer|2012 Krill Dodepo|4|48|Physicist|1997 Mumbo Muveze|8|27|Physicist|2019 Rezrov Zapefaty|3|44|Bookkeeper|2008   Output that should be printed. 10 2 9 7 6 5 1 4 8 3 10 8 9 7 6 5 1 4 2 3 9 8 5 7 6 3 1 4 2 10 8 7 5 4 6 3 1 2 9 10 7 6 5 4 2 3 1 8 9 10 6 4 5 1 2 3 7 8 9 10 5 4 3 1 2 6 7 8 9 10 4 2 3 1 5 6 7 8 9 10 3 2 1 4 5 6 7 8 9 10 2 1 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 It took 62 comparisons to sort this list.

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Hey, i'm try to doing the HeapSort algorithm but I don't get the correct number of comparison..

Below are my codes, small.txt file, and the output that needs to be printed. Thank you!

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>

using namespace std;

class Employee {
public:
//Instantiate
string name;
int id;
int age;
string job;
int year;

//Constructors
Employee() {
name = "";
job = "";
age = year = id = 0;
}
Employee(string name, int id, int age, string job, int year) {
this->name = name;
this->id = id;
this->age = age;
this->job = job;
this->year = year;
}
};
Employee data(string line) {
string input;
stringstream ss(line);
string name;
string job;
int id;
int age;
int year;
getline(ss, name, '|');
getline(ss, input, '|');
id = atoi(input.c_str());//convert the int to string
getline(ss, input, '|');
age = atoi(input.c_str());//convert the int to string
getline(ss, job, '|');
getline(ss, input, '\n');
year = atoi(input.c_str());//convert the int to string
Employee emp(name, id, age, string(job), year);
return emp;
};

//Comparison Counter
int ctr;

void maxHeapify(Employee arr[], int size, int i)
{
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
if (left < size && arr[left].id > arr[largest].id) {
largest = left;
ctr++;
}
if (right < size && arr[right].id > arr[largest].id) {
largest = right;
ctr++;
}

if (largest != i) {
swap(arr[i], arr[largest]);
maxHeapify(arr, size, largest);
}
}

void buildMaxHeap(Employee arr[], int size) {
for (int i = size / 2 - 1; i >= 0; i--) {
maxHeapify(arr, size, i);
}
}

void printResult(Employee arr[], int size) {
for (int m = 0; m < size; m++)
{
cout << arr[m].id << (m + 1 == size ? "\n" : " ");
}
}
void heapSort(Employee arr[], int size)
{
//print before building max heap
printResult(arr, size);

buildMaxHeap(arr, size);

//print after building max heap
printResult(arr, size);

for (int i = size - 1; i > 0; i--) {
swap(arr[0], arr[i]);
maxHeapify(arr, i, 0);
printResult(arr, size);
}
}

int main(int argc, char const* argv[])
{
//Catches error conditions Check if filename included as parameter
if (argc != 2) {
cout << "You need to include a filename on the command line.\n";
return -1;
}

string filename = string(argv[1]);//Fetches the file name
fstream file(filename, ios::in);

//Catches error conditions Check if file exist
if (!file) {
cout << "Error opening file: " << filename << endl;
return -1;
}
int size;
string input;//store the inputs or the lines from the file
file >> size;
getline(file, input);//read the entire file line by line
Employee* employees = new Employee[size];
for (int i = 0; i < size; ++i)
{
getline(file, input);
employees[i] = data(input);
}
heapSort(employees, size);
cout << "It took " << ctr << " comparisons to sort this list.\n";

file.close();
return 0;
}

small.txt file

10
Antharion Buxyho|10|31|Engineer|2020
Molly Nyxujijo|2|42|Custodian|2016
Grubbo Totexyca|9|47|Tester|2011
Thriff Cosapeka|7|24|Technician|2019
Grubbo Neferele|6|61|Director|1995
Borphee Rozuzy|5|23|Director|2019
Jeearr Heposiko|1|32|Engineer|2012
Krill Dodepo|4|48|Physicist|1997
Mumbo Muveze|8|27|Physicist|2019
Rezrov Zapefaty|3|44|Bookkeeper|2008

 

Output that should be printed.

10 2 9 7 6 5 1 4 8 3 10 8 9 7 6 5 1 4 2 3 9 8 5 7 6 3 1 4 2 10 8 7 5 4 6 3 1 2 9 10 7 6 5 4 2 3 1 8 9 10 6 4 5 1 2 3 7 8 9 10 5 4 3 1 2 6 7 8 9 10 4 2 3 1 5 6 7 8 9 10 3 2 1 4 5 6 7 8 9 10 2 1 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 It took 62 comparisons to sort this list.

Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY