We got distracted by the rotation algorithm in gcc’s libstdc++, but let’s get back to the cycle decomposition algorithm in clang’s libcxx . Rotation revisited: Cycle decomposition in clang’s libcxx by Raymond Chen From the article: The implementation in clang’s libcxx performs the minimum number of swaps, roughly n /2, where n is the total number of elements. It does so by viewing the rotation as a permutation and walking through each of the cycles.