如何实现以下OrderElements
功能?
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
当您可以使用线性额外空间时很容易,但是否可以只使用恒定的额外空间来完成,即直接chars
就地对元素进行排序?
PS:这不是考试题;我实际上需要这个功能。
澄清:似乎对所需的元素最终顺序存在误解。示例中的结果数组应具有以下元素,引用原始chars
数组:
{chars[2], chars[4], chars[3], chars[0], chars[1]}
这是
{'c', 'e', 'd', 'a', 'b'}.