The following algorithm can sort three variables x, y and z of type K which are comparable using operator<
x
y
z
K
operator<
void sort(int& a, int& b, int& c) { swap(a, min(a, min(b, c))); swap(b, min(b, c)); }
2 swaps, 3 comparisons.