Question: in C++, show screenshots of code and output, annotate code The program is the same as shown at the end of the Merge sort section,

in C++, show screenshots of code and output, annotate code

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, read_nums(), instead of defining a specific list. Output of the list has been moved to the function print_nums(). An output has been added to merge_sort(), 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 the program 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: 3 2 1 5 9 8 the output is: unsorted: 3 2 1 5 9 8 0 2 | 3 5 0 1 | 2 2 0 0 | 1 1 3 4 | 5 5 3 3 | 4 4 sorted: 1 2 3 5 8 9 comparisons: 8

#include using namespace std;

// Create an array of size ints. // Read size ints from cin, storing them in the array. // Return the array. int* ReadNums(int size) { int *nums = new int[size]; // Create array for (int i = 0; i < size; ++i) { // Read the numbers cin >> nums[i]; } return nums; // Return the array }

// Output the numbers in nums, separated by spaces. // No space or newline will be output before the first number or after the last. void PrintNums(int nums[], int size) { for (int i = 0; i < size; ++i) { cout << nums[i]; if (i < size - 1) { // No space after last number cout << " "; } } cout << endl; }

void Merge(int numbers[], int i, int j, int k) { int mergedSize; int mergePos; int leftPos; int rightPos; int* mergedNumbers = nullptr;

mergePos = 0; mergedSize = k - i + 1; leftPos = i; rightPos = j + 1; mergedNumbers = new int[mergedSize];

while (leftPos <= j && rightPos <= k) { if (numbers[leftPos] < numbers[rightPos]) { mergedNumbers[mergePos] = numbers[leftPos]; ++leftPos; } else { mergedNumbers[mergePos] = numbers[rightPos]; ++rightPos; } ++mergePos; }

while (leftPos <= j) { mergedNumbers[mergePos] = numbers[leftPos]; ++leftPos; ++mergePos; }

while (rightPos <= k) { mergedNumbers[mergePos] = numbers[rightPos]; ++rightPos; ++mergePos; }

for (mergePos = 0; mergePos < mergedSize; ++mergePos) { numbers[i + mergePos] = mergedNumbers[mergePos]; } delete[] mergedNumbers; }

void MergeSort(int numbers[], int i, int k) { int j;

if (i < k) { j = (i + k) / 2; /* Trace output added to code in book */ cout << i << " " << j << " | " << j + 1 << " " << k << endl;

MergeSort(numbers, i, j); MergeSort(numbers, j + 1, k);

Merge(numbers, i, j, k); } }

int main() { int size; cin >> size; int* numbers = ReadNums(size);

cout << "unsorted: "; PrintNums(numbers, size); cout << endl;

MergeSort(numbers, 0, size - 1);

cout << endl << "sorted: "; PrintNums(numbers, size); return 0; }

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!