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

void AList::InsertionSort(int& comparisons, int&movements) //Precondition: none

ID: 3617372 • Letter: V

Question

void AList::InsertionSort(int& comparisons, int&movements)

//Precondition: none

//Postcondition: elements have been sorted using Insertion Sortalgorithm

{

    comparisons = 0;

    movements = 0;

    int i;

    int hold;

    for (int next = 1; next <numberOfElements; next++) {

        movements++;

        hold =elements[next];

        for (i = next-1; i>= 0; i--) {

           comparisons++;

           if (elements[i] > hold) {

               movements++;

               elements[i+1] = elements[i];

           } else {

               break;

           }

        }

        movements++;

        elements[i+1] =hold;

    }

    ordered = true; // KNOWN to be ordered

}

Explanation / Answer

x.xe="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; list-style-type: none; list-style-position: initial; list-style-image: initial; "> void AList::InsertionSort(int& comparisons, int& movements) //Precondition: none //Postcondition: elements have been sorted using Insertion Sort algorithm {     comparisons = 0;     movements = 0;     int i;     int hold; //set all variables for the insertion sort.     for (int next = 1; next = 0; i--) {             comparisons++;             if (elements[i] > hold) {                 movements++;                 elements[i+1] = elements[i]; //go to the next number in the array