// Parameters: // // #define ELEM_TYPE type of elements // #define COMPARE comparison function for elements (optional) // #define ALLOC allocation function (optional) // #define DEALLOC deallocation function (optional)
#ifdef COMPARE staticinlinevoid FN(Sort)(Array * arr)
{ Int len = arr->len; if (len <= 1) return;
ELEM_TYPE * in = ALLOC(ELEM_TYPE, len);
ELEM_TYPE * out = ALLOC(ELEM_TYPE, len);
memcpy(in, arr->items, sizeof(ELEM_TYPE) * len); Int step = 1; while (step < len) { for (Int i = 0; i < len; i += step * 2) { Int p = i, l = i, r = i + step, lmax = l + step, rmax = r + step; if (rmax > len)
rmax = len; if (lmax > len)
lmax = len; while (l < lmax && r < rmax) { int c = COMPARE(in[l], in[r]); if (c < 0) {
out[p++] = in[l++];
} else {
out[p++] = in[r++];
}
} while (l < lmax) {
out[p++] = in[l++];
} while (r < rmax) {
out[p++] = in[r++];
}
}
ELEM_TYPE * tmp = in;
in = out;
out = tmp;
step += step;
}
DEALLOC(arr->items);
DEALLOC(out);
arr->items = in;
arr->cap = len; // we allocated only len items for 'in'.
} #endif
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.