MemSort
Description
Generic in-place sort over a flat array of fixed-size items. Quicksort with median-of-three pivot, insertion-sort fallback for small partitions, tail-iteration on the larger side to bound stack depth at O(log n). Stable across compilers / platforms because we don’t depend on libc’s qsort.
Parameters
| Name | Direction | Description |
|---|---|---|
base |
in,out | Pointer to the first element of the array. |
n_items |
in | Number of items in the array. |
item_size |
in | Size of each item in bytes. |
cmp |
in | Comparator returning <0, 0, >0 like strcmp. |
Success
Returns; the array is sorted in ascending order per cmp.
Failure
No failure mode. If n_items < 2 or cmp is NULL the call is a no-op.
Usage example (Cross-references)
Usage examples (Cross-references)
- In
Memory.c:389:
}
void MemSort(void *base_, size n_items, size item_size, GenericCompare cmp) {
if (!base_ || !cmp || item_size == 0 || n_items < 2) {
return;- In
Memory.c:436:
u8 *right = base + (i + 1) * item_size;
if (left_n < right_n) {
MemSort(base, left_n, item_size, cmp);
base = right;
n = right_n;- In
Memory.c:440:
n = right_n;
} else {
MemSort(right, right_n, item_size, cmp);
n = left_n;
}- In
Vec.c:408:
}
MemSort(vec->data, vec->length, item_size, comp);
}- In
List.c:205:
}
MemSort(data, item_count, item_size, comp);
node = list->head;
Last updated on