In: Computer Science
In C++
Create a program that uses Selection Sort and Insertion Sort for the National Football League list of current players. It's going to be a big list(over 1000 names). Please identify, in comments, which part of the code is for the Selection Sort and which part of the code is for Insertion Sort. It must have both.
Inputting data from a text file named "Players.txt"
Only want the name of the player(first name then last name), their team name, and position they play.
Example is:
Name | Team | Position |
Patrick Mahomes | Chiefs | Quarterback |
Julio Jones | Falcons | Wide Receiver |
Fletcher Cox | Eagles | Defensive Tackle |
Structure of the input file is:
Patrick Mahomes, Chiefs, Quarterback
Julio Jones, Falcons, Wide Receiver
Fletcher Cox, Eagles, Defensive Tackle
Output both lists separately(please indicate which lists goes with which sorting method) to a text file along with how long it took to go through the Selection Sort and Insertion Sort and how many iterations it took for each.
Thanks.
If you can't do this then can you please let someone else.
Both sort will take n iterations so you meant number of comparisons for both sorting algorithms
codr with comments
#include <bits/stdc++.h>
using namespace std;
struct Player {
string name, team, position;
};
int selectionSort(vector<Player>& data) { // selection sort
int i, j, min_idx, comp = 0;
for (i = 0; i < data.size() - 1; i++) {
min_idx = i;
for (j = i + 1; j < data.size(); j++) {
if (data[j].name < data[min_idx].name) min_idx = j;
comp++;
}
swap(data[min_idx], data[i]);
}
return comp;
}
int insertionSort(vector<Player>& data) { // insertion sort
int i, j, comp = 0;
for (i = 1; i < data.size(); i++) {
j = i;
while (j > 0 && data[j - 1].name > data[j].name) {
swap(data[j - 1], data[j]);
j = j - 1;
comp++;
}
}
return comp;
}
vector<Player> getData(fstream& file) { // read from file
vector<Player> data;
string line;
Player p;
while (getline(file, line)) { // line by line
stringstream ss(line); // read tokens
getline(ss, p.name, ','); // first token
getline(ss, p.team, ','); // second token
getline(ss, p.position, ','); // third token
data.push_back(p);
}
return data;
}
void write(fstream& file, vector<Player> data) {
for (auto p : data) {
file << p.name << "," << p.team << "," << p.position << endl;
}
}
void print(vector<Player>& data) { // print data
for (auto p : data) {
cout << left << setw(20) << p.name << setw(20) << p.team << setw(20)
<< p.position << endl;
}
cout << endl << endl;
}
int main() { // test all funtions
fstream file;
file.open("Players.txt");
auto data = getData(file);
print(data);
vector<Player> data1 = data, data2 = data;
auto start1 = clock();
int comp1 = selectionSort(data1);
auto end1 = clock();
auto start2 = clock();
int comp2 = insertionSort(data2);
auto end2 = clock();
cout << "Selection Sort: \n";
cout << "Time (in sec): " << (end1 - start1) * 1.0 / CLOCKS_PER_SEC << endl;
cout << "Comparisons: " << comp1 << endl << endl;
print(data1);
cout << "Insertion Sort: \n";
cout << "Time (in sec): " << (end2 - start2) * 1.0 / CLOCKS_PER_SEC << endl;
cout << "Comparisons: " << comp2 << endl << endl;
print(data2);
fstream f1, f2;
f1.open("selection.txt", ios::out);
f2.open("insertion.txt", ios::out);
write(f1, data1);
write(f2, data2);
}