The template sort routine in Display 16.3 is based on an algorithm called the selection sort . Another related sorting algorithm is called insertion sort. The insertion sort algorithm is the sort method often used to sort a Bridge hand. Consider each element in turn, inserting it into its proper place among the elements at the start of the array that are already sorted. The element being considered is inserted by moving the larger elements “to the right” to make space and inserting the vacated place. For example, the following shows the steps in a selection sort of an array of int s a . The values of a[0] through a[4] are given on each line. The asterisk marks the boundary between the sorted and unsorted portions of the array.
2 * 5 3 4
2 5 * 3 4
2 3 5 * 4
2 3 4 5 *
First, write an insertion sort function that works for int s. Next, write the template version of this sort function. Finally, test thoroughly using several primitive types, using a type you create with the minimal machinery necessary to use the sort routine.
Sorry the answer is not available at the moment…
If you are able to find the answer, please make sure to post it here. So that your Juniors have smile on their lips and feel happy.
Spread the 'tradition of sharing'.