// Source: "Software Design ...", John A Robinson, Newnes, 2004, page 229. void merge(int *astart, const int *aend, int *bstart, const int *bend, int *out) { while ((astart < aend) && (bstart < bend)) { if (*astart < *bstart) *out++ = *astart++; else *out++ = *bstart++; } while (astart < aend) *out++ = *astart++; while (bstart < bend) *out++ = *bstart++; } int sort(int *array, int n) { int mid; extern int *buffer; // Temporary global storage, same size as original array mid = n/2; if (mid == 0) return(0); sort(array, mid); sort(array+mid, n-mid); merge(array, array+mid, array+mid, array+n, buffer); memcpy(array, buffer, n*sizeof(int)); return(0); }