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

Need help with this sorting problem. Here\'s the starter code TASK 1. Write the

ID: 3750325 • Letter: N

Question

Need help with this sorting problem.

Here's the starter code

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 array A, which is created in the first line of the main function, displayed to screern -solution-bash-62x10 Zavala-MEC-MacBookAir:solution Admins./insertsort 1 25 14 90 82 6 73 42 > 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). TASK 3. Before you write the insertionSort function, take a look at the insert function and answer the following questions QUESTION 1 Which lines from the INSERTION-SORT algorithm is the function implementing? QUESTION 2 A) How would you describe what the insert function does? B) What assumption is made about the array that it takes as argument? For task 3, you must make use of the insert function to sort array A manually. That is you must make as many calls to the insert function as needed to have the array sorted Do not use any loops Uncomment the second block of comments in the main function and run the program Your program should pass the tests (you should not see any error message displayed) TASK 4. Implement the insertionSort function according to the INSERTION-SORT algorithm. Make use of the insert function. Uncomment the third block of comments in the main function and run the program. Your program should pass all tests (you should not see any error message displayed) TASK 5. Create an array with 10 numbers of your choice in random order and call it arrayLatName, where you will substitute LastName for YOUR last name. For example, my array would be called arrayZavala. Then, call the insertionSort function to sort the items in your array. Next, use assert to test that the array is now sorted (using the isSorted function). Finally, you should print your sorted array (using the printArray function)

Explanation / Answer

#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>