Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Help with Merge Sort AT&T 3:31 PM a bbhosted.cuny.edu 3 of 4 TASK 1. Write the c

ID: 3754217 • Letter: H

Question

Help with Merge Sort

AT&T 3:31 PM a bbhosted.cuny.edu 3 of 4 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 Admins./mergesort Original Array: 241293849641 Zavala-MEC-MacBookAir:solution Admins Run the program. You should see the original array displayed to screen QUESTION 1. In a sentence describe what you 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 question:s 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?

Explanation / Answer

ANSWER:

The function takes an array and the size of the array as arguments.

#include <iostream>

#include <cassert>

using namespace std;

void insert(int A[], int i, int key) {

while (i >= 0 && A[i] > key){

A[i + 1] = A[i];

i = i - 1;

}

A[i + 1] = key;

}

void insertionSort(int A[], size_t N) {

for (int i = 1; i < N; i++)

{

int key = A[i];

int j = i-1;

insert(A, j, key);

}

}

bool isSorted(int A[], size_t N){

for(int i = 0; i < N - 1; i++){

if(A[i+1]<A[i])

return false;

}

return true;

}

void printArray(int A[], size_t N){

std::cout<<"{ ";

for(int i = 0; i < N; i++){

std::cout<<A[i]<<" ";

}

std::cout<<"}"<<endl;

}

int main () {

int A[] = {22, 11, 99, 88, 9, 7, 42};

printArray(A, 7);

/*FIRST BLOCK OF COMMENTS*/

assert(!isSorted(A, 7));

//Calls to insert function go here (to sort the array manually)

insert(A, 0, 11);

insert(A, 1, 99);

insert(A, 2, 88);

insert(A, 3, 9);

insert(A, 4, 7);

insert(A, 5, 42);

/*SECOND BLOCK OF COMMENTS*/

assert(isSorted(A, 7));

cout << "Array after sorting: " << endl;

printArray(A, 7);

/*THIRD BLOCK OF COMMENTS*/

int B[] = {222, 111, 999, 888, 99, 77, 422};

insertionSort(B, 7);

assert(isSorted(B, 7));

cout << "Array after sorting: " << endl;

printArray(B, 7);

int arrayName[] = {23, 12, 4, 1, 56, 34, 57, 21, 89, 78};

insertionSort(arrayName, 10);

assert(isSorted(arrayName, 10));

cout << "Array after sorting: " << endl;

printArray(arrayName, 10);

}

Task 1 : See attached code to loop through array and print elements

Task 2 : Added sort function to check is array is sorted

Task 3 :

Question 1 : Lines to put an element in the correct position in the array

Question 2 : 1) Puts an element in the correct position in the array

2) Array is not empty

Task 4 : Added code as attached above

Task 5 : Added code.Please change Array Name from arrayName to array<Your Name>

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote