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

Take the source code below and insert this sorting functions to sort all the fir

ID: 3537360 • Letter: T

Question


Take the source code below and insert this sorting functions to sort all the first names into alphabetical order


Sorting function

void insertionSort(string list[], int listLength)

{

    int firstOutOfOrder, location;

    string temp;

for (firstOutOfOrder = 1; firstOutOfOrder < listLength; firstOutOfOrder++)

{      

if (list[firstOutOfOrder] < list[firstOutOfOrder - 1])

    {

            temp = list[firstOutOfOrder];

            location = firstOutOfOrder;

            do

            {

                list[location] = list[location - 1];

                location--;

            }

            while (location > 0 && list[location - 1] > temp);

            list[location] = temp;

      } //end if

} //end for

} //end insertionSort

Source code

#include <iostream>

#include <string>

using namespace std;

int main ()

{

const int LISTSIZE = 6;

string list [] = {"William Smith ","Tom Reed ","Phillip Richards ","Danny Curtis ","Mary Pope ","Robert Justice "};

cout << " The list has the following names ";

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

cout << list [i];

}

return 0;

}

Explanation / Answer

#include <iostream>


#include <string>

#include<conio.h>


using namespace std;

void insertionSort(string list[], int listLength)


{


int firstOutOfOrder, location;


string temp;



for (firstOutOfOrder = 1; firstOutOfOrder < listLength; firstOutOfOrder++)


{


if (list[firstOutOfOrder] < list[firstOutOfOrder - 1])


{


temp = list[firstOutOfOrder];


location = firstOutOfOrder;



do


{


list[location] = list[location - 1];


location--;


}


while (location > 0 && list[location - 1] > temp);



list[location] = temp;


} //end if


} //end for


} //end insertionSort


int main ()


{


const int LISTSIZE = 6;


string list [] = {"William Smith ","Tom Reed ","Phillip Richards ","Danny Curtis ","Mary Pope ","Robert Justice "};


cout << " The list has the following names ";


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


cout << list [i];


}


insertionSort( list,LISTSIZE );

cout<<endl

cout << " The list has the following names in sorted order ";



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


cout << list [i];


}

getch();


}

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