Modify the merge sort algorithm to sort a vector of employee
Solution
#include <iostream>
#include <vector>
using namespace std;
vector<int> empsal; //employee salaries as vector
void merge(int low, int middle, int high) // merge combined logic
{
int temp[empsal.size()];
for(int i = low; i <= high; i++)
temp[i] = empsal[i];
int i = low,j = middle+1,k = low;
while (i <= middle && j <= high)
{
if(temp[i] <= temp[j])
{
empsal[k] = temp[i];
i++;
}
else
{
empsal[k] = temp[j];
j++;
}
k++;
}
while (i <= middle)
{
empsal[k] = temp[i];
k++;
i++;
}
while (j <= high)
{
empsal[k] = temp[j];
k++;
j++;
}
}
void mergeSort(int low, int high) // merge sort logic
{
if (low < high)
{
int middle = (low + high) / 2;
mergeSort(low, middle); // divide list
mergeSort(middle+1, high); // divide list
merge(low, middle, high); // combined of list
}
}
int main()
{
int b;
do
{
cout<<\"Enter salary of employee / 0 for stop entering\";
cin>>b; //reading salary
empsal.push_back(b); //and stored in vector
mergeSort(0, empsal.size()-1);
for(int i=0;i<empsal.size();i++)
cout<<empsal[i]<<\"\\t\";
}while(b!=0);
return 0;
}

