Block swap algorithm for array rotation in C++


The block swap algorithm for array rotation is an efficient algorithm that is used for array rotation. It can do your work in O(n) time complexity.

So, in array rotation, we are given an array arr[] of size n and a number k that define the no. of the element to be rotated.

Let’s see an example on array rotations −

Input 

arr[] = {4, 6, 1, 8, 9, 2}, k = 2 (number of rotations.)

Output

{1, 8, 9, 2, 4, 6}

Explanation − On rotation, we will shift the one element to the last position and shift the next elements to one position.

Element at index 0 will be shifted to index n-1. And the rest of the elements are shifted to the previous index.

Block Swap Algorithm

The block swap algorithm is used to perform array rotations perfectly.

Algorithm

Step 1 − Divide the array two sub-arrays with k as division point. Let them be X = arr[0...k-1] and Y = arr[k...n-1].

Step 2 − Follow the below steps until the size of X and Y are the same.

Step 2.1 − If the size of X > Y, divide X into two portions X1 and X2 such that size of X1 is equal to the size of Y. Then swap the sub-array X1 and Y. This will change the original array formation from X1X2Y to YX2X1.

Step 2.2 − If the size of Y > X, divide Y into two portions Y1 and Y2 such that the size of Y2 is equal to the size of X. Then swap the subarray X and Y2. This will change the original array formation from XY1Y2 to Y2Y1X.

Step 3 − When the size of X and Y are the same, swap them.

This algorithm needs a repetitive call to the same chunk of code. This repetitive call can be achieved using two approaches. They are the recursive approach and iterative approach. we will discuss the approach using programs.

Example

Program to illustrate Recursive Approach −

#include <iostream>
using namespace std;
void swapSubArray(int arr[], int start, int end, intk){
   int temp;
   for(int i = 0; i < k; i++){
      temp = arr[start + i];
      arr[start + i] = arr[end + i];
      arr[end + i] = temp;
   }
}
void blockSwapAlgo(int arr[], int k, int n) {
   if(k == 0 || k == n)
      return;
   if(k<(n-k)) {
      swapSubArray(arr, 0, (n-k), k);
      blockSwapAlgo(arr, k, (n-k));
   }
   else if(k>(n-k)){
      swapSubArray(arr, 0, k, (n-k));
      blockSwapAlgo((arr+n-k), (2*k-n), k);
   }
   else{
      swapSubArray(arr, 0, (n-k), k);
      return;
   }
}
int main() {
   int arr[] = {4, 6, 1, 8, 9, 2};
   int size = sizeof(arr) / sizeof(arr[0]);
   int k = 3;
   cout<<"Array before rotations :\t";
   for(int i = 0; i<size; i++)
      cout<<arr[i]<<" ";
   blockSwapAlgo(arr, k, size);
   cout<<"\nArray after rotating "<<k<<" times :\t";
   for(int i = 0; i<size; i++)
      cout<<arr[i]<<" ";
   return 0;
}

Output

Array before rotations : 4 6 1 8 9 2
Array after rotating 3 times : 8 9 2 4 6 1

Example

Program to illustrate iterative Approach −

 Live Demo

#include <iostream>
using namespace std;
void swapSubArray(int arr[], int start, int end, int k){
   int temp;
   for(int i = 0; i < k; i++){
      temp = arr[start + i];
      arr[start + i] = arr[end + i];
      arr[end + i] = temp;
   }
}
void blockSwapAlgoIt(int arr[], int k, int size) {
   int i, j;
   if(k == 0 || k == size)
      return;
   i = k;
   j = size - k;
   while (i != j) {
      if(i < j){
         swapSubArray(arr, k-i, k+j-i, i);
         j -= i;
      }
      else{
         swapSubArray(arr, k-i, k, j);
         i -= j;
      }
   }
   swapSubArray(arr, k-i, k, i);
}
int main() {
   int arr[] = {4, 6, 1, 8, 9, 2};
   int size = sizeof(arr) / sizeof(arr[0]);
   int k = 3;
   cout<<"Array before rotations :\t";
   for(int i = 0; i<size; i++)
      cout<<arr[i]<<" ";
   blockSwapAlgoIt(arr, k, size);
   cout<<"\nArray after rotating "<<k<<" times :\t";
   for(int i = 0; i<size; i++)
      cout<<arr[i]<<" ";
   return 0;
}

Output

Array before rotations : 4 6 1 8 9 2
Array after rotating 3 times : 8 9 2 4 6 1

Updated on: 05-Aug-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements