previous | start | next

1.1 Template Function Example

Write a simple function to sort an array of elements of type T:



void simplesort(T a[], int n)
{
  int maxpos;
  for(int i = n; i > 1; i--) {
    maxpos = 0;
    for(int j = 1; j < i; j++) {
      if ( a[maxpos] < a[j] ) {
        maxpos = j;
      }
    }
    T tmp = a[maxpos];
    a[maxpos] = a[i-1];
    a[i-1] = tmp;
  }
}


previous | start | next