Here is the scenario.
I am given an array \'A\' of integers. The size of the array is not fixed. The function that I am supposed to write may be called once with an
Executable pseudo-code (aka Python): thenumbers.sort(key=str)
. Yeah, I know that using Python is kind of like cheating -- it's just too 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 key=
argument to Python's sort implements.
In more detail (pseudocode):
aux
as long as the numbers
arraylength of numbers-1
, aux[i]=stringify(numbers[i])
indices
of the same lengthlength of numbers-1
, indices[i]=i
indices
, using as cmp(i,j)
strcmp(aux[i],aux[j])
results
of the same lengthlength of numbers-1
, results[i]=numbers[indices[i]]
results
over numbers
aux[i]
, and also aux
, indices
, results