r*****e 发帖数: 792 | 1 void arrayShuffle(vector &v, int first, int last) {
int mid, halfLen=(last-first)/2;
if (last-first<=2)//already in place
return;
if (last-first==4) {//swap middle two
swap(v[first+1], v[first+2]);
return;
}
/* regular cases */
mid = halfLen/2+first;
vector::iterator it = v.begin();
reverse(it+mid, it+mid+halfLen);//reverse entire mid section first
reverse(it+mid, it+mid+(mid-first));
reverse(it+mid+(mid-first), it+mid+halfLen);
a... 阅读全帖 |
|