qsort template

We all heard of qsort somewhere in our software education. It is an interesting recursive algorithm for sorting a bunch of items. If you've not read the expandable article I suggest that it be read now for the comments on templates.

This implementation of qsort is a template and will operate on any array provide the entity in the array, i.e. a class Data {...} includes the following operations (methods, functions):

  • *p == *q
  • *p > *q
  • *p <= *q
  • *s = *p
where p and q are defined as Data* p; Data* q; This achieved by calling the template qsort as follows:
qsort(&array[firstIndex], &array[lastIndex]); // or equivalent
qsort only sorts arrays (even expandable arrays) where p is the first argument and q is the second argument. The firstIndex is the index of the first entity to be sorted. The lastIndex is the index of the last entity to be sorted.

The qsort algorithm is fastest for large arrays but around 15 elements an insertion sort is faster so this version of qsort reverts to the simpler sort algorithm when it reaches 15 elements.