Easy Selection

array int messy, sorted;  // these have come from somewhere & must have equal length
int sortedIx, searchIx, min, minLoc;
// this also assumes the system defines a number BIGNUM that's bigger
// than anything in messy.

// outer loop over the sorted array -- when everything's sorted we're done
for (sortedIx = 0; sortedIx < sorted.length; sortedIx++) {
// initialize min to something bogus
min = BIGNUM; minLoc = -1;
// inner loop over messy array: find the smallest number in messy.
for (searchIx = 0; searchIx < search.length; searchIx++) {
// if it's the smallest yet seen.
if (messy[searchIx] < min) {
min = messy[searchIx];
minLoc = searchIx;
}
} // for searchIx
// now stick that small value in sorted
sorted[sortedIx] = min;
// and make sure it won't get found again
messy[minLoc] = BIGNUM;
} // for sortedIx