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++ For your program, you will choose either the Selection Sort algorithm or the Insertion Sort...
c++ For your program, you will choose either the Selection Sort algorithm or the Insertion Sort algorithm and create a recursive implementation of that algorithm. Your program should: Randomly generate an array of at least 20 values. Display the contents of that (unsorted) array. Use the recursive implementation of either Selection or Insertion Sort to sort the values. Display the contents of the now sorted array, to demonstrate the success of the algorithm.
write a program in C language Create a function to perform the insertion sort. Now the...
write a program in C language Create a function to perform the insertion sort. Now the program will perform the following steps: Prompt the user to enter the number of array elements (say, N). Read the number of elements (N). Use dynamic memory allocation to allocate an array of N single precision floating-point elements (C type float). Read the N single precision floating-points elements to the allocated array. Invoke a function to sort the array using insertion sort (the insertion...
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.
This is an exercise in correctly implementing insertion sort and selection sort. This assignment includes a...
This is an exercise in correctly implementing insertion sort and selection sort. This assignment includes a text data file containing information on tutorial websites for a variety of programming languages. The data file is named Tutorials. It contains records of programming tutorial websites. The record structure for this text file is: FIELD 1 = Programming Language FIELD 2 = Name and Description of Website FIELD 3 = URL Web Address of Language Tutorial The structure of the file is that...
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT