Given a list of numbers and a function that returns Low, Medium, or High, sort the list by Lows, then Mediums, then Highs.
Variation of dutch flag

 Do a O(n) pass and find the number of lows, mids and highs
 Start low =0 , mid = low and high = low+mid pointers at the respective positions
 Increment them if the value under them is in the respective category if not swap with the pointer of the right pointer and increment the pointer, so on

Threeway partitioning: https://en.wikipedia.org/wiki/Dutch_national_flag_problem

int numbers[] = {10, 3, 5, 7, 9, 15, 1, 2, 8, 14, 6, 4, 9, 13, 22}; int tlow = 5, thigh = 10; int nlow, nmed, nhigh, len, tmp; len = sizeof(numbers)/sizeof(int); nlow = 0; nmed = 0; nhigh= len  1; while (nmed < nhigh) { if (numbers[nmed] < tlow) { tmp = numbers[nmed]; numbers[nmed] = numbers[nlow]; numbers[nlow] = tmp; nlow++; } else if (numbers[nmed] > thigh) { tmp = numbers[nmed]; numbers[nmed] = numbers[nhigh]; numbers[nhigh]= tmp; nhigh; } else { nmed++; } }