Question: In c++ please. Modify this base code to implement the first sorting algorithm shown in this video using vectors and sorting from largest number to
In c++ please. Modify this base code to implement the first sorting algorithm shown in this video using vectors and sorting from largest number to smallest and count number of comparisons while sorting using the global variable in the base code.
VIDEO LINK:
https://www.youtube.com/watch?v=cVMKXKoGu_Y&feature=emb_logo
BASE CODE START:
// sorting items from the heaviest to lightest // by selecting the heaviest item at each time #include #include using namespace std; // global variable that returns the number of comparisons to sort given input int comparisons = 0; ostream& operator<<(ostream& out, vector& v) { // overload the output operator to display elements of v return out; } void sort_heaviest(vector& v){ int heaviest; // implement the sorting algorithm based on video } int main() { cout << "//////Test 1 for vector v ///////////////////////////////"<< endl; vector v{10,9,8,7,6,5,4,3,2,1}; cout << "initial vector v: "; // use overloaded output operator to display vector's elements // use comma to separate the vector's elements cout << endl; // call the sorting function for vector v cout << "# of comparisons to sort v: " << comparisons << endl << endl; cout << "vector v after sorting: "; // use overloaded output operator to display elements of sorted vector // use comma to separate the vector's elements cout << endl; cout << "//////Test 2 for vector v1 ///////////////////////////////"<< endl; vector v1{1,2,3,4,5,6,7,8,9,10}; cout << "initial vector v1: "; // use overloaded output operator to display vector's elements // use comma to separate the vector's elements cout << endl; // call the sorting function for vector v1 cout << "# of comparisons to sort v1: " << comparisons << endl << endl; cout << "vector v1 after sorting: "; // use overloaded output operator to display elements of sorted vector // use comma to separate the vector's elements cout << endl; } Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
