title: Sort array by pairwise difference
body: <p>For example we have the array <code>X[n] = {X0, X1, X2, ... Xn}</code>
  The goal is to sort this array that the difference between every pair is in ascending order.</p>
  
  <p>For example <code>X[] =  {10, 2, 7, 4}</code></p>
  
  <p>Answers are:</p>
  
  <pre><code>2 7 10 4
  4 10 7 2
  </code></pre>
  
  <p>I have some code but it's brute force :)</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  int main(int argc, char **argv)
  {
      int array[] = { 10, 2, 7, 4 };
      int a[4];
  
      for(int i = 0; i &lt; 4; i++){
          a[0] = array[i];
  
          for(int j = 0; j &lt; 4; j++){
              a[1] = array[j];
              if(a[0] == a[1])
                 continue;
  
              for(int k = 0; k &lt; 4; k++){
                  a[2] = array[k];
                  if(a[0] == a[2] || a[1] == a[2])
                      continue;
  
              for(int l = 0; l &lt; 4; l++){
                  a[3] = array[l];
                  if(a[0] == a[3] || a[1] == a[3] || a[2] == a[3])
                      continue;
                  if(a[0] - a[1] &lt; a[1] - a[2] &amp;&amp; a[1] - a[2] &lt; a[2] - a[3])  
                      printf("%d %d %d %d\n", a[0], a[1], a[2], a[3]);
               }
           }
       }
     }
      return 0;
   }
  </code></pre>
  
  <p>Any idea for "pretty" algorithm ? :)</p>