From Leonard J. Moss of SLAC:
Here's a hybrid QuickSort I wrote a number of years ago. It's
based on suggestions in Knuth, Volume 3, and performs much better
than a pure QuickSort on short or partially ordered input arrays.
SUBROUTINE SORTRX(N,DATA,INDEX)
SORTRX -- SORT, Real input, indeX output
Input: N INTEGER
DATA REAL
Output: INDEX INTEGER (DIMENSION N)
This routine performs an in-memory sort of the first N elements of
array DATA, returning into array INDEX the indices of elements of
DATA arranged in ascending order. Thus,
DATA(INDEX(1)) will be the smallest number in array DATA;
DATA(INDEX(N)) will be the largest number in DATA.
The original data is not physically rearranged. The original order
of equal input values is not necessarily preserved.
SORTRX uses a hybrid QuickSort algorithm, based on several
suggestions in Knuth, Volume 3, Section 5.2.2. In particular, the
"pivot key" [my term] for dividing each subsequence is chosen to be
the median of the first, last, and middle values of the subsequence;
and the QuickSort is cut off when a subsequence has 9 or fewer
elements, and a straight insertion sort of the entire array is done
at the end. The result is comparable to a pure insertion sort for
very short arrays, and very fast for very large arrays (of order 12
micro-sec/element on the 3081K for arrays of 10K elements). It is
also not subject to the poor performance of the pure QuickSort on
partially ordered data.
Created: 15 Jul 1986 Len Moss |