C++ Example: A Generic Sort

This section defines a generic sort function.

Listing 7.11, SelectionSort.cpp, gives a function to sort an array of double values. Here is a copy of the function:

1 void selectionSort(double list[], int listSize)
2 {
3   
for (int i = 0; i < listSize; i++)
4    {
5       
// Find the minimum in the list[i..listSize-1] 6       double currentMin = list[i];
7       
int currentMinIndex = i;
8
9       
for (int j = i + 1; j < listSize; j++)
10      {
11         
if (currentMin > list[j])
12         {
13            currentMin = list[j];
14            currentMinIndex = j;
15         }
16      }
17
18     
// Swap list[i] with list[currentMinIndex] if necessary
19      if (currentMinIndex != i)
20      {
21          list[currentMinIndex] = list[i];
22          list[i] = currentMin;
23      }
24   }
25 }

It is easy to modify this function to write new overloaded functions for sorting an array of int values, char values, string values, and so on. All you need to do is to replace the word double by int, char, or string in two places (lines 1 and 6).

Instead of writing several overloaded sort functions, you can define just one template func­tion that works for any type. Listing 12.3 defines a generic function for sorting an array of elements.

Listing 12.3 GenericSort.cpp

1 #include <iostream>
2
#include <string>
3
using namespace std;
4
5
template<typename T>
6
void sort(T list[], int listSize)
7 {
8   
for (int i = 0; i < listSize; i++)
9    {
10     
// Find the minimum in the list[i..listSize-1] 11      T currentMin = list[i];
12     
int currentMinIndex = i;
13
14     
for (int j = i + 1; j < listSize; j++)
15      {
16         
if (currentMin > list[j])
17         {
18             currentMin = list[j];
19             currentMinIndex = j;
20         }
21      }
22
23     
// Swap list[i] with list[currentMinIndex] if necessary;
24      if (currentMinIndex != i)
25      {
26          list[currentMinIndex] = list[i];
27          list[i] = currentMin;
28      }
29    }
30 }
31
32
template<typename T>
33
void printArray(const T list[], int listSize)
34 {
35   
for (int i = 0; i < listSize; i++)
36    {
37        cout << list[i] <<
” “;
38    }
39    cout << endl;
40 }
41
42
int main()
43 {
44   
int list1[] = {3, 5, 1, 0, 2, 8, 7};
45    sort(list1,
7);
46    printArray(list1,
7);
47
48   
double list2[] = {3.5, 0.5, 1.4, 0.4, 2.5, 1.8, 4.7};
49    sort(list2,
7);
50    printArray(list2,
7);
51
52    string list3[] = {
“Atlanta”, “Denver”, “Chicago”, “Dallas”};
53    sort(list3,
4);
54    printArray(list3,
4);
55
56   
return 0;
57 }

Two template functions are defined in this program. The template function sort (lines 5-30) uses the type parameter T to specify the element type in an array. This function is identical to the selectionSort function except that the parameter double is replaced by a generic type T.

The template function printArray (lines 32-40) uses the type parameter T to specify the element type in an array. This function displays all the elements in the array to the console.

The main function invokes the sort function to sort an array of int, double, and string values (lines 45, 49, 53) and invokes the printArray function to display these arrays (lines 46, 50, 54).

Source: Liang Y. Daniel (2013), Introduction to programming with C++, Pearson; 3rd edition.

Leave a Reply

Your email address will not be published. Required fields are marked *