Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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;
}
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Principles Programming And Performance

Authors: Patrick O'Neil

1st Edition

1558603921, 978-1558603929

More Books

Students also viewed these Databases questions

Question

How are fraud examiners used as expert witnesses?

Answered: 1 week ago

Question

5. Identify the logical fallacies, deceptive forms of reasoning

Answered: 1 week ago

Question

6. Choose an appropriate organizational strategy for your speech

Answered: 1 week ago