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

template <typename T> void insertionSort(T list[], int listLength) { int firstOu

ID: 3739659 • Letter: T

Question

template <typename T>

void insertionSort(T list[], int listLength)

{

        int firstOutOfOrder, location;

        T 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 of if statement

        } // end of for-loop

} // end of insertionSort template

When given the what is above... [PLEASE ANSWER FULLY]

a) Assume dList[50] declares an array of 50 doubles, and is properly initialized. Write a statement to call template function defined above to sort the array.

b) To use the template function defined above to sort an array of class objects, the class must overload what operator so the template function can work properly?

Explanation / Answer

(a) insertionSort(dList, 50);

You can call a template function by just passing any parameter. No need to specify double or int or any other data type.

(b) We'd have to overload the less than(<) operator