/*------------------------------------------- * File name: selsort.temp * Author: Xiannong Meng * Date: April 2, 1997 * Assignment: Classroom demonstration * Problem statement: This is the template file for selection sort. * The function can be applied to any data type that the comparison * operations ">", "==", and "<" are defined. *------------------------------------------- */ // function declaration for 'swap' template void swap(T &x, T &y); // function definition for SelectSort // It takes two parameters, the size of the array 's' // and the array itself 'a[]' // It returns the sorted array 'a[]' template void SelectSort(int s, T a[]) { T min; int minIndex; int i, j; for (i = 0; i < s-1; i ++) { min = a[i]; minIndex = i; for (j = i+1; j < s; j ++) { if (a[j] < min) { min = a[j]; minIndex = j; } } if (minIndex != i) swap(a[i], a[minIndex]); } } // swap two items x and y template void swap(T &x, T &y) { T temp; temp = x; x = y; y = temp; }