Javatpoint Logo
Javatpoint Logo

Block Swap Algorithm for Array Rotation in C++

In this article, you will learn about the block swap algorithm for array rotation in C++ with its example. But before going to its implementation, you must know about the array rotation.

Rotations in C++:-

A basic operation in programming and computer science is array rotation. It entails moving components in an array to the left or right to alter their order. The Block Swap Algorithm is one of the most effective ways to rotate an array among other strategies.

Overview of Array Rotation:-

Array rotation is a prominent issue in numerous applications, including optimization and data manipulation. It can be applied to a number of real-world issues, such as moving items in a list or rotating an image. The Block Swap Algorithm in C++ can efficiently carry out this operation.

The Algorithm of Block Swap:

When rotating an array in O(n) time-where n is the number of items in the array, the Block Swap Algorithm is a clever and effective solution. The Block Swap Algorithm technique is based on the idea of splitting the array into blocks and swapping these blocks to accomplish the desired rotation.

Let's examine the Block Swap Algorithm's fundamental steps:-

  • Divide the array into two blocks.
  • The remaining elements are in the other block, while the first d elements (where d is the number of places to rotate) are in the first
  • In other words, move the first d elements to the end of the array and the remaining elements to the front by switching the two blocks.


Let's take an example to illustrate the use of a block swap algorithm for array rotation in C++.


Block Swap Algorithm for Array Rotation in C++

Code Explanation:

  • In this code, the swap function switches the 'd' elements between the array's 'start' and 'end' locations. It makes the swaps using a temporary variable.
  • The leftRotate function determines whether the rotation count 'd' is equal to the array's size 'n' or zero. If this is the case, the function returns without modifying the array and no rotation is required.
  • The function converts negative rotation values to positive values to handle them. By doing this, the proper direction of rotation is ensured.
  • For rotation, there are two instances to take into account:
  • A block swap between the initial 'd' elements and the last 'n - d' elements is more efficient if 'd' is bigger than 'n - d'. It is because it reduces the quantity of swaps that are necessary.
  • It is more effective to execute a block swap between the first 'd' elements and the first 'n - d' elements if 'd' is less than or equal to 'n - d'.
  • The leftRotate function is called recursively on the sub-array that begins at position 'n - d' with a rotation count of '2 * d - n' in the first scenario after a block swap between the relevant blocks. The remaining rotation is handled by this recursive call.
  • In the second scenario, a block swap is executed, followed by a recursive call to the leftRotate function on the sub-array with a rotation count of 'n - d', starting at position '0'. The remaining rotation is handled by this recursive call.
  • The leftRotate function is used in the main function to execute a left rotation of 'd' places around an example array.
  • Lastly, the console receives a print of the rotated array.


In C++, the Block Swap Algorithm is an effective method for rotating arrays. It provides a prompt solution to a typical programming issue. An array can be rotated with linear time complexity using the Block Swap Algorithm by splitting it up into blocks and then swapping them. Therefore, it is a great option in scenarios when performance is essential.

In conclusion, we have looked at the C++ Block Swap Algorithm for array rotation. In addition to being effective, this approach is a useful resource for programmers to have in their toolbox when handling array manipulation problems. Your ability to manage array rotations efficiently can be significantly improved by comprehending and putting this algorithm into practice.

Next TopicC++ thread_local

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA