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
array int sorta; // this comes from somewhere only one now!
int sortedIx, searchIx, min, minLoc;
int temp; // add a temporary location for swapping values.
// don't need BIGNUM anymore...
// outer loop over the sorted array -- when everything's sorted we're done
for (sortedIx = 0; sortedIx < sorta.length; sortedIx++) {
// now min is set to the thing in the slot we're currently trying
// to fill.
min = sorta[sortedIx]; minLoc = sortedIx;
// inner loop over messy part of the array: find the smallest number
for (searchIx = sortedIx; searchIx < search.length; searchIx++) {
// if it's the smallest yet seen.
if (sorta[searchIx] < min) {
min = sorta[searchIx];
minLoc = searchIx;
}
} // for searchIx
// move the old value to temp
temp = sorta[sortedIx];
// now stick that small value in the right place
sorta[sortedIx] = min;
// finish the swap
sorta[minLoc] = temp;
} // for sortedIx
array int sorta; // this comes from somewhere in a messy state
int boundaryIx, searchingIx; // these are indecies
int currentItem // this is the temporary storage for th item we're moving...
// it's old home gets filled with stuff making space for it.
// outer loop over the sorted array -- when everything's sorted we're done
for (boundaryIx gets the values 1 through sorta.length) {
// save new item
currentItem = sorta[boundaryIx]
// inner loop starts at boundary and works down
for (searchingIx = boundaryIx - 1; (searchingIx >= 0) AND (sorta[searchingIx] > currentItem);
searchIx--) {
// haven't found the right place yet or wouldn't be in here, so
// just move stuff.
sorta[searchingIx+1] = sorta[searchingIx];
} // for searchingIx
// if we're out of the loop, so searchingIx is pointing just neg. of where we want to be
sorta[searchingIx+1] = currentItem;
} // for boundaryIx
QuickSort(int sorta[], first, last)
int pivotIx, Split();
// when first >= last, we're through -- we've sorted the smallest possible sized bin
if (first < last) {
// This is the real work, and it returns the location for the pivot point
pivotIx = Split(sorta, first, last);
// Now the recursive calls
QuickSort(sorta, first, pivotIx-1);
QuickSort(sorta, pivotIx+1, last);
} // if first < last -- all done!
// must have type integer since returns pivotIx
int Split(int sorta[], first, last)
int topIx, botIx; // indecies
int pivotVal, temp; // items
pivotVal = sorta[first]; // start with the middle number (sort of arbitrarily)
botIx = first, topIx = last+1; // start looking just past each end (see why below)
while (true) {
// look for a small number above the pivot (or the pivot)
while (sorta[--topIx] > pivotVal AND topix > botix) {};
// now look for a big number below the pivot (or the pivot)
while (sorta[++botIx] < pivotVal AND topix > botix) {};
// if botix and topix have met, you're done
if (botIx >= topIx) { break;} // stops the while (true) loop
//otherwise, switch the two misaligned things you have found
temp = sorta[topIx];
sorta[topIx] = sorta[botIx];
sorta[botIx] = temp;
} // while forever (well, until the break)
//now we've also found the location for the pivot...
temp = sorta[topIx]; // we know this is smaller than or equal to the pivot
sorta[topIx] = sorta[first]; // so we swap it with the pivot
sorta[first] = temp;
return(topIx);
(defun split (sorta first last)
(let ((topix (+ last 1))
(botix first)
(pivotval (aref sorta first)))
(catch 'done
(loop
(setf topix (do ((tix (- topix 1) (- tix 1)))
((or (>= botix tix)
(<= (aref sorta tix) pivotval))
tix)))
(setf botix (do ((bix (+ botix 1) (+ bix 1)))
((or (>= bix topix)
(>= (aref sorta bix) pivotval))
bix)))
(if (>= botix topix) (throw 'done sorta))
(let ((temp (aref sorta topix)))
(setf (aref sorta topix) (aref sorta botix))
(setf (aref sorta botix) temp))
))
(let ((temp (aref sorta first)))
(setf (aref sorta first) (aref sorta topix))
(setf (aref sorta topix) temp))
sorta ; should return topix not sorta to be part of qsort
))