Before answering any specific question, please note that my goal is not to randomly move the array, but to perfect shuffle, like the one that the ideal dealer can do with a set of cards, that is, split the deck in half and perform one pass in randomly (alternating a card from one half of the deck with one card from the other half). (This is actually one exercise from algorithms in the third edition of C: Sedgewick: nbr 11.3 p. 445)
So, I'm not interested in algorithms like Fisher-Yates shuffle, etc.
Said that I want to avoid using any auxiliary array when performing shuffling, the code that I was able to deliver is as follows:
template<typename T>
void two_way_shuffle(vector<T>& data,int l,int r)
{
int n=(r-l)+1;
int m=(r+l)/2;
if(n%2==0) ++m;
int s,p{l+1};
for(;p<=r;p+=2,m++)
{
s=data[m];
for(int i{m};i>p;i--)
data[i]=data[i-1];
data[p]=s;
}
}
template<typename T>
void two_way_unshuffle(vector<T>& data,int l,int r)
{
int n=(r-l)+1;
if(n%2!=0){
--r;
--n;
}
int m=(r+l)/2;
int s,p{l+1},w{r-1};
for(;p<=w;p++,w--)
{
s=data[w];
for(int i{w};i>p;i--)
data[i]=data[i-1];
data[p]=s;
}
for(p=l+1,w=m;p<w;p++,w--)
swap(data[p],data[w]);
}
, "p", , "w", , , "p", , [p] 's'. .. ( )
FROM: 12345678
TO: 15263748
FROM: IS THIS WORKING CORRECTLY OR NOT?
TO: ICSO RTRHEICST LWYO ROKRI NNGO T?
FROM: SHUFFLING TEST
TO: SNHGU FTFELSIT
unshuffle, , , clever . , , ( , ).
, .
, unshuffle? , ?