Block Swap Algorithm For Array Rotation In C++ - C++ Programming Tutorial
C++ Course / STL Algorithm / Block Swap Algorithm For Array Rotation In C++

Block Swap Algorithm For Array Rotation In C++

BLUF: Mastering Block Swap Algorithm For Array Rotation In C++ is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Block Swap Algorithm For Array Rotation In C++

C++ is renowned for its efficiency. Learn how Block Swap Algorithm For Array Rotation In C++ enables low-level control and high-performance computing in the tutorial below.

In this guide, you will discover the block interchange technique for rotating arrays in C++ along with an illustrative example. Prior to delving into its execution, understanding array rotation is essential.

Rotations in C++:-

A fundamental concept in programming and computer science involves array rotation, which involves shifting elements within an array either to the left or right in order to change their arrangement. One of the efficient techniques for rotating an array is the Block Swap Algorithm, which stands out as a highly effective method when compared to other approaches.

Overview of Array Rotation:-

Array rotation is a significant challenge in various scenarios, like optimization and data handling. It finds utility in a wide array of practical situations, like rearranging elements in a sequence or rotating an image. The Block Swap Algorithm implemented in C++ is adept at effectively executing this task.

The Algorithm of Block Swap:

When performing array rotation in O(n) time, where n represents the array's item count, the Block Swap Algorithm presents a smart and efficient approach. This technique involves dividing the array into blocks and exchanging these blocks to achieve the intended 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.
  • Program:

Let's consider an example to demonstrate the application of a block interchange algorithm for array rotation in C++.

Example

#include <iostream>
void swap(int arr[], int start, int end, int d) {
 for (int i = 0; i < d; i++) {
 int temp = arr[start + i];
 arr[start + i] = arr[end + i];
 arr[end + i] = temp;
 }
}
void leftRotate(int arr[], int n, int d) {
 if (d == 0 || d == n) return;
 if (d < 0) {
 d = n + d; // Handle negative rotation values
 }
 if (d > n - d) {
 swap(arr, 0, n - d, d);
 leftRotate(arr + n - d, d, 2 * d - n);
 } else {
 swap(arr, 0, n - d, d);
 leftRotate(arr, n - d, d);
 }
}
int main() {
 int arr[] = {1, 2, 3, 4, 5, 6, 7};
 int n = sizeof(arr) / sizeof(arr[0]);
 int d = 2;
 leftRotate(arr, n, d);
 for (int i = 0; i < n; i++) {
 std::cout << arr[i] << " ";
 }
 return 0;
}

Output:

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.
  • Conclusion:

In C++, the Block Swap Technique serves as a powerful approach for rotating arrays efficiently. This method offers a quick resolution to a common programming challenge. By dividing the array into blocks and interchanging them, the Block Swap Algorithm enables rotation with a time complexity of O(n). Hence, it proves to be a valuable choice in situations where optimal performance is crucial.

To summarize, we have examined the C++ Block Swap Algorithm for rotating arrays. Besides its efficiency, this method serves as a valuable tool for developers dealing with array manipulation challenges. Understanding and implementing this algorithm can greatly enhance your proficiency in managing array rotations.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience