Question

In: Computer Science

In C++ Create a program that uses Selection Sort and Insertion Sort for the National Football...

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.

Solutions

Expert Solution

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

}


Related Solutions

Write a program in C++ to test either the selection sort or insertion sort algorithm for...
Write a program in C++ to test either the selection sort or insertion sort algorithm for array-based lists as given in the chapter. Test the program with at least three (3) lists. Supply the program source code and the test input and output. List1: 14,11,78,59 List2: 15, 22, 4, 74 List3: 14,2,5,44
C++ --------------------------------------------- Do a comparison of a slow sort with Big O(n2) (selection sort, insertion sort,...
C++ --------------------------------------------- Do a comparison of a slow sort with Big O(n2) (selection sort, insertion sort, or bubble sort) and one faster sort of Big O(n * log n) (mergesort or quicksort). Count the number of moves (a swap counts as one move). With mergesort, you can count the range of the part of the array you are sorting (i.e. last-first+1). Use the code from the textbook (copy from the lecture notes) and put in an extra reference parameter for...
PROVIDE CODE ONLY IN C++ / NO OTHER LANGUAGES PLEASE ADD SELECTION SORT/ INSERTION SORT/ AND...
PROVIDE CODE ONLY IN C++ / NO OTHER LANGUAGES PLEASE ADD SELECTION SORT/ INSERTION SORT/ AND BUBBLE SORT FUNCTION TO THIS PROGRAM #include <iostream> #include<vector> #include <algorithm >   #include <chrono>    #include <ctime> using namespace std; void bubblesSort() { // Please create Bubble Sort function// Make another for Selection Sort and  Insertion Sort } int main() { // empty vector vector<int> data; // data [0], data [1]... data[N-1] <-- end(data) // set of values to test N for (auto N :...
The Binary Insertion Sort Algorithm is a variation of the Insertion Sort Algorithm that uses a...
The Binary Insertion Sort Algorithm is a variation of the Insertion Sort Algorithm that uses a binary search technique rather than a linear search technique to insert the ith element in the correct place among the previously sorted elements. (i) Express the Binary Insertion Sort Algorithm in pseudocode. (ii) Compare the number of comparisons of elements used by the Insertion Sort Algorithm and the Binary Insertion Sort Algorithm when sorting the list (7,4,3,8,1,5,4,2). (iii) Show that the Insertion Sort Algorithm...
give a good explanation of Bubble sort, Insertion sort, Selection sort, and Quicksort.
give a good explanation of Bubble sort, Insertion sort, Selection sort, and Quicksort.
Write Insertion Sort and Bubble Sort Program for C# also write their algorithm and Explain their...
Write Insertion Sort and Bubble Sort Program for C# also write their algorithm and Explain their working.
Sort the following set of numbers using bubble sort, insertion sort, and selection sort. Show the...
Sort the following set of numbers using bubble sort, insertion sort, and selection sort. Show the process step-by-step, and find the time complexity in Big-O notation for each method. For sorting, use ascending order. 49, 7, 60, 44, 18, 105
For this assignment, find out how to do a bubble sort, selection sort, or insertion sort...
For this assignment, find out how to do a bubble sort, selection sort, or insertion sort in Java. You have the option to choose but you must label (with comments) the algorithm you choose to implement. Convert that algorithm to a generic algorithm and constraint it to only using numerics. Your method should accept an array as a parameter and sort the content of the array. If you wish, you can throw an exception if the contents of the array...
Which of the following sorting algorithms are stable: insertion sort, selection sort, merge sort and quick...
Which of the following sorting algorithms are stable: insertion sort, selection sort, merge sort and quick sort? Give a simple scheme that makes any sorting algorithm stable. How much additional time and space does your scheme entail?
(code in C++ language) [Code Bubble sort, Insertion sort Create a Big array with random numbers....
(code in C++ language) [Code Bubble sort, Insertion sort Create a Big array with random numbers. Record the time. Run Bubble Check time (compute the processing time) do it 100 times (random numbers) Take the average Insertion: Compare] (some explanations please)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT