Question: Need help with merge sort task! Please answer all the questions in the task below. Starter code I was given: #include #include #include using namespace
Need help with merge sort task!
Please answer all the questions in the task below.


Starter code I was given:
#include#include #include using namespace std; /* Display contents of the input array to screen A: Input Array N: Size of the array A */ void printArray(int A[], size_t N){ //You must implement this function } /* Checks whether the items in the input array are sorted A: Input Array N: Size of the array A */ bool isSorted(int A[], size_t N){ //You must implement this function return true; } /* Merge Procedure Assumes that the subarrays A[p..q] and A[q+1..r] are sorted It merges them to form a single sorted subarray that replaces the current subarray A[p..r] */ void merge(int A[], size_t p, size_t q, size_t r) { int n1 = q - p + 1; int n2 = r - q; int Left[n1]; int Right[n2]; for (int i =0; i TASK 1. Write the code for the printArray function. The function takes an array and the size of the array as arguments. The function should print to screen the contents of the array it receives as argument. The contents of the array should be printed on a single line Run the program. You should see the contents of array A (which is created in the first line of the main function) displayed to screen solution-bash-80x9 Zavala-MEC-MacBookAir:solution Admin$ ./mergesort Original Array: 241293849641 Zavala-MEC-MacBookAir:solution AdminsI Run the program. You should see the original array displayed to screen QUESTION 1. In a sentence describe what vou think the tests in the first block of comments are supposed to do TASK 2. Write the code for the isSorted function. It should return true if the items in the array it receives as argument are sorted (in ascending order), and false otherwise Uncomment the first block of comments in the main function and run the program. The isSorted function should pass the test (you should not see any error message displayed) QUESTIONS Before you write the mergeSort function, you need to understand the merge function provided. The function implements the pseudocode of the MERGE procedure given above (page 31 of your textbook). Answer the following questions 2. Other than differences in the names of the left and right arrays and some indexes (arrays start at 0 in C++ and the textbook starts them at 1), what significant differences do you notice between the MERGE pseudocode in the book and the merge function provided with the starter code? 3. In the pseudocode, what is the purpose of putting sentinels at the ends of the 4. Why aren't lines 8-9 of the MERGE pseudocode implemented in the merge 5. Why is a while loop used in the merge function instead of the for loop in lines 12- 6. Why are there two extra while loops at the end of the merge function? And why arrays L and R (lines 8-9)? function? 17 of the MERGE pseudocode? aren't they in the MERGE pseudocode? TASK 1. Write the code for the printArray function. The function takes an array and the size of the array as arguments. The function should print to screen the contents of the array it receives as argument. The contents of the array should be printed on a single line Run the program. You should see the contents of array A (which is created in the first line of the main function) displayed to screen solution-bash-80x9 Zavala-MEC-MacBookAir:solution Admin$ ./mergesort Original Array: 241293849641 Zavala-MEC-MacBookAir:solution AdminsI Run the program. You should see the original array displayed to screen QUESTION 1. In a sentence describe what vou think the tests in the first block of comments are supposed to do TASK 2. Write the code for the isSorted function. It should return true if the items in the array it receives as argument are sorted (in ascending order), and false otherwise Uncomment the first block of comments in the main function and run the program. The isSorted function should pass the test (you should not see any error message displayed) QUESTIONS Before you write the mergeSort function, you need to understand the merge function provided. The function implements the pseudocode of the MERGE procedure given above (page 31 of your textbook). Answer the following questions 2. Other than differences in the names of the left and right arrays and some indexes (arrays start at 0 in C++ and the textbook starts them at 1), what significant differences do you notice between the MERGE pseudocode in the book and the merge function provided with the starter code? 3. In the pseudocode, what is the purpose of putting sentinels at the ends of the 4. Why aren't lines 8-9 of the MERGE pseudocode implemented in the merge 5. Why is a while loop used in the merge function instead of the for loop in lines 12- 6. Why are there two extra while loops at the end of the merge function? And why arrays L and R (lines 8-9)? function? 17 of the MERGE pseudocode? aren't they in the MERGE pseudocode
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
