1 min readAug 5, 2020
Hello. I understand which algorithm of rotation you mean. I call it algorithm of Jon Bentley because I have seen it in his book. Bentley’s algorithm has worse speed than algorithms described above. It is 4 times slower than the Reverse. The Swap is faster than the Reverse in 5–6 time. And in addition it destroys cashes by design because it does random access to all the array. And it’s code looks not so elegant and simple as the others. So I decided don’t describe it here.
You can try to test it yourself. Here is the code:
template < class T >
void left_rotate(vector<T> & v, size_t rot_dist) {
const size_t v_size = v.size();
const size_t GCD = gcd(rot_dist, v_size);
for(size_t i = 0; i < GCD; ++i) {
auto curr = i;
auto temp = v[curr];
while(true) {
auto next = curr + rot_dist;
if(next >= v_size) { next = next - v_size; }
if(next == i ) { break; }
v[curr] = v[next];
curr=next;
}
v[curr] = temp;
}
}