Question: Can you please fix the code!! ( Should be in c + + ) The code is in the pictures added. 1 5 . 1

Can you please fix the code!! (Should be in c++) The code is in the pictures added. 15.13 LAB: Merge sort
The program is the same as shown at the end of the Merge sort section, with the following changes:
Numbers are entered by a user in a separate helper function, ReadNums(), instead of defining a specific array in main(). The first number is how many integers to be sorted, and the rest are the integers.
Output of the array has been moved to the helper function PrintNums().
An output has been added to MergeSort(), showing the indices that will be passed to the recursive function calls.
Add code to the merge sort algorithm to count the number of comparisons performed.
Add code at the end of main() that outputs "comparisons: " followed by the number of comparisons performed (Ex: "comparisons: 12")
Hint: Use a global variable to count the comparisons.
Note: Take special care to look at the output of each test to better understand the merge sort algorithm.
Ex: When the input is:
6321598
the output is:
unsorted: 321598
02|35
01|22
00|11
34|55
33|44
sorted: 123589 #include
#include
using namespace std;
int comparisons =0; // Global variable to count comparisons
void Merge(vector& numbers, int left, int mid, int right){
int n1= mid - left +1;
int n2= right - mid;
vector L(n1), R(n2);
for (int i =0; i n1; i++)
L[i]= numbers[left + i];
for (int j =0; j n2; j++)
R[j]= numbers[mid +1+ j];
int i =0, j =0, k = left;
while (i n1 && j n2){
if (L[i]= R[j]){
numbers[k++]= L[i++];
} else {
numbers[k++]= R[j++];
}
comparisons++; // Increment comparison count
}
while (i n1){
numbers[k++]= L[i++];
}
while (j n2){
numbers[k++]= R[j++];
}
}
void MergeSort(vector& numbers, int left, int right){
if (left right){
int mid = left +(right - left)/2;
cout left "" mid "|"(mid +1)"" right endl;
MergeSort(numbers, left, mid);
MergeSort(numbers, mid +1, right);
Merge(numbers, left, mid, right);
}
}
void ReadNums(vector& numbers){
int size;
cin >> size;
numbers.resize(size);
for (int i =0; i size; ++i){
cin >> numbers[i];
}
}
void PrintNums(const vector& numbers){
for (int num : numbers){
cout num "";
}
cout endl;
}
int main(){
vector numbers;
ReadNums(numbers);
cout "unsorted: ";
PrintNums(numbers);
cout endl;
MergeSort(numbers,0, numbers.size()-1);
cout endl;
cout "sorted: ";
PrintNums(numbers);
cout "comparisons: " comparisons endl;
return 0;
}
 Can you please fix the code!! (Should be in c++) The

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!