import "util/sort";
There are two variants of the sort algorithms. The general sorting algorithm is slower but preserves the original order of the equal values. It is preferred to use the quick sort variant whenever possible as it is much faster and doesn't allocate any extra memory.
The general sorting algorithm has an advantage when sorting by multiple different keys as it can be sorted repeatedly. However you can achieve the same result using the quick sort algorithm if you compare among all the keys in a single comparison.
You can use the provided macros for better performance (the comparison and swap code fragments will get inlined into a specialized sorting function).
macro array_swap(array, idx1: Integer, idx2: Integer)
macro array_sort(array, cmp_expr)
macro array_sort_range(array, off: Integer, len: Integer, cmp_expr)
array
- reference to the arrayindex1
- index of the first elementindex2
- index of the second elementvalue1
- value of the first elementvalue2
- value of the second element
macro array_quick_sort(array, cmp_expr)
macro array_quick_sort_range(array, off: Integer, len: Integer, cmp_expr)
true
when the first element is smaller than the second. The order among equal values is undefined.
macro sort_algorithm(array, off: Integer, len: Integer, data, cmp_expr, swap_code)
array
and
data
are made available to the expressions. The comparison expression
must produce negative integer when the first element is smaller than the second,
positive integer when the first element is bigger than the second, or zero when
they are equal.array
- reference to the array (or any other kind of type)data
- reference to the extra passed dataindex1
- index of the first elementindex2
- index of the second elementmacro quick_sort_algorithm(array, off: Integer, len: Integer, data, cmp_expr, swap_code)
true
when the first element is smaller than the second. The order among equal values is undefined.
function array_sort_ints(array: Integer[])
function array_sort_ints(array: Integer[], off: Integer, len: Integer)
function array_sort_floats(array: Float[])
function array_sort_floats(array: Float[], off: Integer, len: Integer)
function array_sort_strings(array: String[])
function array_sort_strings(array: String[], off: Integer, len: Integer)
function array_sort_custom(array, cmp_func, data)
function array_sort_custom(array, off: Integer, len: Integer, cmp_func, data)
function array_quick_sort_custom(array, cmp_func, data)
function array_quick_sort_custom(array, off: Integer, len: Integer, cmp_func, data)
true
when the first element is smaller than the second. The order among equal values is undefined.
function general_sort(array, cmp_func, swap_func, data)
function general_sort(array, off: Integer, len: Integer, cmp_func, swap_func, data)
function general_quick_sort(array, cmp_func, swap_func, data)
function general_quick_sort(array, off: Integer, len: Integer, cmp_func, swap_func, data)
true
when the first element is smaller than the second. The order among equal values is undefined.