\index{sorting!arrays}
GENERIC INTERFACEArraySort (Elem);
WhereElem.T
is a type that is not an open array type andElem
contains
PROCEDURE Compare(a, b: Elem.T): [-1 .. 1];Compare
must define a total order. Any parameter mode may be used.
PROCEDURE Sort(VAR a: ARRAY OF Elem.T; cmp := Elem.Compare);
Sort the elements ofa
using the order defined bycmp
.
END ArraySort.
Sort(a, cmp)
permutes the elements of a
such that:
FIRST(a) <= i < j <= LAST(a)implies
cmp(a[i], a[j]) <= 0.The algorithm used is QuickSort: \begin{itemize} \item It is not stable. \item On average, it requires
O(N ln N)
comparison and assignment
operations. In the worst case it may require O(N*N)
operations.
\end{itemize}
For an expanded description of QuickSort, see \cite{Sedgewick:Alg}.