How to sort 4 (or more) values with non-piecewise functions?

82 Views Asked by At

This question was inspired by this:

Finding a non-piece wise function that gives us the $i$'th largest number.

My question is how to do this for four or more values.

In other words, given 4 values $a, b, c,$ and $d$, specify functions $order_i(a, b, c, d)$ for $i = 1 $ to $4$ such that $order_i(a, b, c, d)$ returns the $i^{th}$ smallest value.

Here is my start as an answer for 4 values:

Define a set of auxiliary functions

$bmin2(a, b) =\frac12(a+b-|a-b|) $, $bmax2(a, b) =\frac12(a+b+|a-b|) $, $bmin3(a, b, c) =bmin2(bmin2(a, b), c) $, $bmax3(a, b, c) =bmax2(bmax2(a, b), c) $, $bmin4(a, b, c, d) =bmin2(bmin3(a, b, c), d) $, $bmax4(a, b, c, d) =bmax2(bmax3(a, b, c), d) $, $bcenter(a, b, c, d) =a+b+c+d-bmax4(a, b, c, d)-bmin4(a, b, c, d) $.

We get the min and max, and we can get the sum of the middle two, but how to separate that sum into the individual values so we can we can decide which is smaller is not immediately clear to me.

As to doing this for 5, oy!

And, for general $n$, I have no idea.

1

There are 1 best solutions below

3
On BEST ANSWER

$max (a,b,c,d)= max(max(a,b),max(c,d))$

$min(a,b,c,d)=min(min(a,b),min(c,d))$

the second largest object is $min(max(a,b,c),max(a,c,d),max(a,b,d),max(b,c,d))$

the second smallest is $max(min(a,b,c),min(b,c,d),min(a,c,d))$

All of the previous functions can be expressed as polynomials.

Apply a Lagrange polynomial to put everything together.


There is a more general approach for $n$ objects.If you want to find the $k'th$ largest element you can just take the min over the max of all the subsets of size $n-k+1$. The subsets with the smallest max is precisely the one which does not have any of the largest $k-1$ elements, and hence the max of that subset is the $k$ largest number.