
Finish the code algorthims
#include <iostream>
#include <vector>
using namespace std;
//Todo4: Add cout statement to MergeSort () to print out
// a line for each time this function is called, show the parameter
// a line for each time we return from this function, show the parameter
//Todo3: add a global variable to keep track
// frequency of copy operation (marked below), and comparison operation (marked below)
//
/* Merge two sorted
@param l1, l2: two sorted vectors
@param list: the vector to hold the merging result
@pre-condcition: elements in l1, l2 are in ascending order
@post-condcition: list contains elements from l1 and l2, in ascending order */
void BinaryMergeSortedList (const vector<int> l1, const vector<int> l2, vector<int> & list)
{
//Idea: as well as l1 and l2 both have elements left to be copied to list,
// compare the "front runner", and copy the smaller one to next slot in list.
// When only one of l1, l2 have elements left, copy all remaining elements to list
// one by one.
//make sure list has exact same # of elements as l1, l2 combined:
// (by dropping extra or adding 0 to the end)
// subsequently, we can use list[k] or list.at(k) to refer to each element...
// more efficient than:
// 1) first: list.clear(); //make list an empty list
// 2) and then: list.push_back(l1[i++]); //to add a new element into
// the back (end of list).
list.resize(l1.size()+l2.size());
int i,j,k; //used to index l1, l2 and list respectively
i=j=k=0; //start from first position
while (i<l1.size() && j < l2.size()){
if (l1[i] <= l2[j]) //comparison operation
//copy smaller one to list, and advance indices
list[k++] = l1[i++]; //copy operation
else
list[k++] = l2[j++]; //copy operation
}
//copy remaining elements to list
while (i<l1.size()) //if l1 is not done yet
list[k++] = l1[i++]; //copy operation
while (j<l2.size()) //if l2 is not done yet
list[k++] = l2[j++]; //copy operation
}
/* sort the vector into ascending order
@param list: the vector of int to be sorted
@pre-condiction: list has been initialized with some data
@post-condition: the integers stored in list are rearranged into
ascending order */
void MergeSort (vector<int> & list)
{
if (list.size()==1)
return;
//general case: divide and conquer
//divide part
int n=list.size();
int mid = (0+n-1)/2; //averge of first and last index
vector<int> leftHalf(list.begin(),list.begin()+mid+1);
// range constructor: initilize with elements from a range of existing vector:
// from first iterator (including) to last iterator (not included)
//https://www.cplusplus.com/reference/vector/vector/vector/
vector<int> rightHalf(list.begin()+mid+1,list.end());
//conquer part: solve the two subproblems recursively (and independently from
// each other
MergeSort (leftHalf);
MergeSort (rightHalf);
//Combine part: merge data from the two sorted halves
// back into list, in ascending order
BinaryMergeSortedList (leftHalf, rightHalf, list);
}
//Todo 3: split list into n vectors of size 1
// add them into a queue of vectors
// enter a loop to take two vectors from the queue, do a binaryMergeSortedList,
// push the resulting vector back into the queue
// ...
void MergeSortIterative(vector<int> & list)
{
//Todo:
}
int main()
{
vector<int> a{6,4,3,8,2,5,1,7};
MergeSort (a);
cout<<"result:";
//use iterator to go through vector a
//for (vector<int>::iterator it=a.begin(); it<a.end(); it++)
//cout <<*it <<" ";
for (auto element:a) //supported in C++11, element's type is auto
cout <<element<<" ";
cout <<endl;
//Todo 1:
// Measuring running time of MergeSort for n=10,20,50,100,500,...
// and frequency of comparison & copy operation
}

Trending nowThis is a popular solution!
Step by stepSolved in 2 steps

- C++ Sort a vector Define a function named SortVector that takes a vector of integers as a parameter. Function SortVector() modifies the vector parameter by sorting the elements in descending order (highest to lowest). Then write a main program that reads a list of integers from input, stores the integers in a vector, calls SortVector(), and outputs the sorted vector. The first input integer indicates how many numbers are in the list. Ex: If the input is: 5 10 4 39 12 2 the output is: 39,12,10,4,2, For coding simplicity, follow every output value by a comma, including the last one. Your program must define and call the following function:void SortVector(vector<int>& myVec) #include <iostream>#include <vector>using namespace std; /* Define your function here */ int main() { /* Type your code here */ return 0;} Enter program input - 5 10 4 39 12 2arrow_forwardQuestion 5 Write the code for the function printRoster, it should print out the contacts in a format similar to the example. You can use a range-based for-loop and auto to simplify the solution, if you wish. #include #include #include #include #include using namespace std; void printRoster(map>>& roster) { // Fill code here int main() { map>> roster; roster["ELCO"]["CS-30"]. emplace_back("Anthony Davis"); roster["ElCo"]"cS-30"j. emplace_back("Talen Horton-Tucker"); roster["ElCo"Ï"BUS-101"].emplace_back("LeBron James"); roster["SMC"]["CHEM-101"].emplace_back("Russell Westbrook"); printRoster(roster); return 0; Here is the sample output: ElCo BUS-101: LeBron James CS-30: Anthony Davis Talen Horton-Tucker SMC CHEM-101: Russell Westbrookarrow_forward1. Write a function invert_dict to reverse a dictionary, for example: >>> eng2sp = {'one': 'uno', 'two': 'dos', 'three': 'tres'} >>> sp2eng = invert_dict(eng2sp) >>> sp2eng {'uno':'one', 'dos':'two', 'tres':'three'} 2. Write a function print_dict to print all the key-values in a dictionary, for example: >>> d2 {'a': 'one', 'b': 'two', 'c': 'Three'} >>> print_dict(d2) a:one b:two c:Threearrow_forward
- Description You are provided a skeleton code in the file student_code.c. Some functions are completely implemented, and some are partially implemented. Additionally, you can add your own functions if required. Complete the program as per following details so that we can have functionality as described in the Synopsis above. All the code in single C file. REMEMBER DO NOT USE sleep(). 1. The given code reads the file's content for you and populates a dynamic array of type struct thread with information about the threads. The threads have filled in this list of threads for you. Additionally, you can check the threadCount variable to see how many threads were created using the input file. If more members are needed, you can add them. You might be able to initialise those extra members during file read if you wish to. 2. The main() already contains the code to read threads from input file. However, there is no code to initialize, execute and synchronize threads. You have to perform these…arrow_forwardplease help me with codingarrow_forwardComplete the C++ code below#include "std_lib_facilities.h"/*1. Define a function which calculates the outer product of two vectors. The function return is a matrix. */vector<vector<int>> outerProduct(vector<int>& A, vector<int>& B){//implement here}/*2. Define a function which transposes a matrix. */vector<vector<int>> transpose(vector<vector<int>>& A){//implementation}/*3. Define a function which calculates the multiplication of a matrix and it's transpose. */vector<vector<int>> product(vector<vector<int>>& A){// implementation}/*4. Define a print out function that will print out a matrix in the following format:Example: a 3 by 4 matrix should have the print out:1 2 3 42 3 4 53 4 5 6*/void printMatrix(vector<vector<int>>& A){// implementation}int main(){// Define both vector A and vector Bvector<int> A = {1 ,2, 3, 4, 5};vector<int> B = {1, 2, 3};/*Test outerProduct…arrow_forward
- need help in C++ Problem: You are asked to create a program for storing the catalog of movies at a DVD store using functions, files, and user-defined structures. The program should let the user read the movie through the file, add, remove, and output movies to the file. For this assignment, you must store the information about the movies in the catalog using a single vector. The vector's data type is a user-defined structure that you must define on functions.h following these rules: Identifier for the user-define structure: movie. Member variables of the structure "movie": name (string), year (int), and genre (string). Note: you must use the identifiers presented before when defining the user-defined structure. Your solution will NOT pass the unit test cases if you do not follow the instructions presented above. The main function is provided (you need to modify the code of the main function to call the user-defined functions described below). The following user-defined functions are…arrow_forwardC++ Please help!arrow_forwardUse iterators to print each element of v separted by spaces. #include <iostream>#include <vector> using namespace std; int main(){vector<int> v = {1, 3, 5}; (.....) }arrow_forward
- You may insert an element into an arbitrary position inside a vector using an iterator. Write a function, insert(vector, value) which inserts value into its sorted position inside the vector. The function returns the vector after it is modified. #include <vector>using namespace std; vector<int>& insert(vector<int>& v, int value){ ................ return v; }arrow_forwardC++.Write a function, named SecondNegative, that takes a const reference to a vector of doubles, it should return an const_iterator to the element in the vector that is the second value that is negative. If no such element exists, have the iterator point to one past the end.arrow_forwardC++arrow_forward
- Computer Networking: A Top-Down Approach (7th Edi...Computer EngineeringISBN:9780133594140Author:James Kurose, Keith RossPublisher:PEARSONComputer Organization and Design MIPS Edition, Fi...Computer EngineeringISBN:9780124077263Author:David A. Patterson, John L. HennessyPublisher:Elsevier ScienceNetwork+ Guide to Networks (MindTap Course List)Computer EngineeringISBN:9781337569330Author:Jill West, Tamara Dean, Jean AndrewsPublisher:Cengage Learning
- Concepts of Database ManagementComputer EngineeringISBN:9781337093422Author:Joy L. Starks, Philip J. Pratt, Mary Z. LastPublisher:Cengage LearningPrelude to ProgrammingComputer EngineeringISBN:9780133750423Author:VENIT, StewartPublisher:Pearson EducationSc Business Data Communications and Networking, T...Computer EngineeringISBN:9781119368830Author:FITZGERALDPublisher:WILEY





