Given that Crystal has no array-sorting functions, there is often a need to write one. An insertion sort is the most efficient of the particularly simple and short sort algorithms, working well with short sets, whether they are already substantially sorted or not.
Shared NumberVar Array foo;
Local NumberVar i := 2;
Local NumberVar j;
Local NumberVar length := ubound(foo);
Local NumberVar value;
//
while ( i <= length )
do
(
value := foo [i];
j := i - 1;
while ( j >= 1 and foo [j] > value )
do
(
foo [j + 1] := foo [j];
j := j - 1;
);
foo [j + 1] := value;
i := i + 1;
);
An example report is available which demonstrates this sort algorithm.
Attachment | Size |
---|---|
insertion-sort.rpt_.gz (3.02 KB) | 3.02 KB |