How do I implement the following OrderElements
function?
char chars[] = {'a', 'b', 'c', 'd', 'e'};
int want_order[] = {2, 4, 3, 0, 1};
int length = 5;
OrderElements(chars, want_order, length);
// chars now contains: c, e, d, a, b
It's easy when you can use linear extra space, but can it be done with only constant extra space, i.e., directly sorting the chars
elements in-place?
P.S.: This was not an exam question; I actually need this function.
CLARIFICATION: There seems to be a misunderstanding about the desired final order of elements. The resulting array in the example should have the following elements, referring to the original chars
array:
{chars[2], chars[4], chars[3], chars[0], chars[1]}
which is
{'c', 'e', 'd', 'a', 'b'}.