|
Sorts
items,
which must be an array of strings when
compare
is omitted, and an array of objects otherwise,
into ascending order using the quicksort algorithm
developed by C. A. R. Hoare in 1962.
The current Yoix implementation was borrowed directly from
"The C Programming Language" by B. W. Kernighan and D. M. Ritchie.
The
qsort
built-in does not impose restrictions on the elements in the
items
array, but the algorithm assumes they can be rearranged,
and that can sometimes result in a
typecheck
error when
items
is a heterogeneous array.
When
compare
is supplied it must be a function (or built-in) or a pointer to a function (or built-in)
that takes exactly two arguments, which can be any two objects in
items,
and returns an integer less than, equal to, or greater than zero
to indicate the first argument is less than, equal to, or greater
than the second argument.
If
*compare
is a function (or built-in) and
compare
itself is a
Dictionary
or any other object that associates names with values, then
compare
is used as the function's local context.
In other words,
compare
will be used to resolve
this
and it will also be searched immediately before the function's global context.
When
compare
is omitted
qsort
uses an internal comparison function that, except for the fact that
it accepts
NULL
strings as arguments, behaves exactly like the
compareTo
built-in.
The optional
count
argument divides the
items
array into groups of
count
elements, starting at index 0;
elements at the end of
items
that do not completely fill a new group are ignored.
Only the first element in each group participates in comparisons, but
qsort
moves entire groups as it rearranges the
items
array.
Optional array arguments that are supplied after
items
are automatically rearranged by
qsort
in parallel with
items,
and each one must be at least as large as
items.
The
items
argument is sorted in place and also available as the return value.
| |
| Example: |
The program,
import yoix.stdio.*;
import yoix.string.*;
import yoix.util.*;
Array keys = {
"Now is",
"the time",
"for",
"all",
"good men"
};
qsort(keys, compareTo);
printf("sorted: %O\n", keys);
sorts the array of strings into ascending order using the
compareTo
built-in and prints the results on standard output.
In this example you get the same result,
and you get it a little faster, if you omit the comparison
function argument in the
qsort
call.
| | |
| Return: |
Array
| | |
| See Also: |
compareTo,
strcmp,
uniq
|
|