Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Executable pseudo-code (aka Python): <code>thenumbers.sort(key=str)</code>. Yeah, I know that using Python is kind of like cheating -- it's just <strong>too</strong> powerful;-). But seriously, this also means: if you can sort an array of strings lexicographically, as Python's sort intrinsically can, then just make the "key string" out of each number and sort that auxiliary array (you can then reconstruct the desired numbers array by a str->int transformation, or by doing the sort on the indices via indirection, etc etc); this is known as DSU (Decorate, Sort, Undecorate) and it's what the <code>key=</code> argument to Python's sort implements.</p> <p>In more detail (pseudocode):</p> <ol> <li>allocate an array of char** <code>aux</code> as long as the <code>numbers</code> array</li> <li>for i from 0 to <code>length of numbers-1</code>, <code>aux[i]=stringify(numbers[i])</code></li> <li>allocate an array of int <code>indices</code> of the same length</li> <li>for i from 0 to <code>length of numbers-1</code>, <code>indices[i]=i</code></li> <li>sort <code>indices</code>, using as <code>cmp(i,j)</code> <code>strcmp(aux[i],aux[j])</code></li> <li>allocate an array of int <code>results</code> of the same length</li> <li>for i from 0 to <code>length of numbers-1</code>, <code>results[i]=numbers[indices[i]]</code></li> <li>memcpy <code>results</code> over <code>numbers</code></li> <li>free every <code>aux[i]</code>, and also <code>aux</code>, <code>indices</code>, <code>results</code></li> </ol>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload