这个可以通过swap来实现。最坏情况下需要swap n 次. we can keep a counter, if
the current position does not need to swap, then increase the counter by one
. if it needs swap, we can swap once, but this swap will make sure that
destination position will not be swapped again, then at least one element is
fixed.
void swapArray(vector &A, vector &R, int i, int j){
swap(A[i], A[j]);
swap(R[i], R[j]);
}
void inPlacePermutation(vector &A, vector &R){
int cnt = 0;
int i = 0;
while (cnt < A.size()) {
if(R[i] == i) {
cnt++;
i++;
}else{
swapArray(A, R, i, R[i]);
cnt;
}
}
return;
}