Answered step by step
Verified Expert Solution
Question
1 Approved Answer
#ifndef _ SORTER _ H #define _ SORTER _ H #include #include #include using namespace std; template void sorter ( vector &vec, int k )
#ifndef SORTERH
#define SORTERH
#include
#include
#include
using namespace std;
template
void sortervector &vec, int k
write your solution for kway merge sort below
#endif
Instructions:
KWay Merge Sort: Implementation details and hints
The input array will be in the form of a vector. The signature of the function you will implement is given below:
template
void sortervector &vec, int k;
For this project your sorting code should go in the file sorter.h because we want you to implement a sorting function template to be able to sort vectors of any data type as long as the comparison operator, is defined for that type.
In order to be able to call this function recursively, you need to create k new vectors inside the function and call sorter on them. After the smaller vectors are sorted, you need to merge all k of them and store the sorted values back on your input vector, vec.
Splitting k ways
In the first part of your function, you need to make k new vectors containing roughly nk elements where n is the size of the original input vector You probably want to store these in another vector a vector of vectors There are many ways to achieve this splitting step. Two approaches are outlined below.
In the first approach, use math to decide how big each of the new vectors need to be Note that, unless n is some multiple of k performing the integer division nk will yield a truncated result. Eg suppose n and k : the truncated result for is However, if we create vectors of size we leave out elements! So our vectors should be sizes and Note, though, we can easily figure out how many vectors need an additional element by using the modulo operator:
In the second approach, we use a "roundrobin" approach to assign elements to our collection of vectors. In this approach, simply loop over all of the elements in the original input vector; the first element should go in the first of your k vectors, the next in the second vector, and so forth. You can easily keep track of where you are in the k vectors and properly "wrap around" back to the start using modulo math: x k for integer x always returns a value between and k
kway merge
There are a number of ways you can implement the merging of k smaller sorted vectors. Two approaches are outlined below. Note: for this project we are not implementing kway merge in the most efficient fashion. This is intentional. You may find more efficient methods described online do not use these! For the next analysis project, you will be analyzing kway merge as described for this project, and performing timing experiments. If you implement a more efficient algorithm, you will be unable to collect the desired results for the analysis project.
Approach
Approach : minimum elements first
One way is to keep k different indices eg in a vector to keep track of where you are in each of the sorted vectors during the merge operation.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started