Javascript Program for Block swap algorithm for array rotation
Last Updated :
19 Sep, 2023
Write a function rotate(ar[], d, n) that rotates arr[] of size n by d elements.

Rotation of the above array by 2 will make array

Algorithm :
Initialize A = arr[0..d-1] and B = arr[d..n-1]
1) Do following until size of A is equal to size of B
a) If A is shorter, divide B into Bl and Br such that Br is of same
length as A. Swap A and Br to change ABlBr into BrBlA. Now A
is at its final place, so recur on pieces of B.
b) If A is longer, divide A into Al and Ar such that Al is of same
length as B Swap Al and B to change AlArB into BArAl. Now B
is at its final place, so recur on pieces of A.
2) Finally when A and B are of equal size, block swap them.
Recursive Implementation:
JavaScript
<script>
let leftRotate = (arr, d, n) =>{
/* Return If number of elements to be rotated
is zero or equal to array size */
if(d == 0 || d == n)
return;
/*If number of elements to be rotated
is exactly half of array size */
if(n - d == d) {
arr = swap(arr, 0, n - d, d);
return;
}
/* If A is shorter*/
if(d < n - d) {
arr = swap(arr, 0, n - d, d);
leftRotate(arr, d, n - d);
}
else{
/* If B is shorter*/
arr = swap(arr, 0, d, n - d);
/*This is tricky*/
leftRotate(arr + n - d, 2 * d - n, d);
}
}
/*UTILITY FUNCTIONS*/
/* function to print an array */
let printArray = (arr, size) =>{
ans = ''
for(let i = 0; i < size; i++)
ans += arr[i]+" ";
document.write(ans)
}
/*This function swaps d elements
starting at index fi
with d elements starting at index si */
let swap = (arr, fi, si, d) =>{
for(let i = 0; i < d; i++) {
let temp = arr[fi + i];
arr[fi + i] = arr[si + i];
arr[si + i] = temp;
}
return arr
}
// Driver Code
arr = [1, 2, 3, 4, 5, 6, 7];
leftRotate(arr, 2, 7);
printArray(arr, 7);
</script>
Output:
3 5 4 6 7 1 2
Time Complexity: O(N), where N represents the size of the given array.
Auxiliary Space: O(N), due to recursive stack space.
Iterative Implementation:
Here is iterative implementation of the same algorithm. Same utility function swap() is used here.
JavaScript
<script>
// JavaScript code for above implementation
function leftRotate(arr, d, n)
{
if(d == 0 || d == n)
return;
let i = d;
let j = n - d;
while (i != j)
{
if(i < j)
{
// A is shorter
arr = swap(arr, d - i, d + j - i, i);
j -= i;
}
else{ // B is shorter
arr = swap(arr, d - i, d, j);
i -= j;
}
}
arr = swap(arr, d - i, d, i);
// This code is contributed by rohitsingh04052.
}
</script>
Time Complexity: O(N), where N represents the size of the given array.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Please see following posts for other methods of array rotation:
https://www.geeksforgeeks.org/array-rotation/
https://www.geeksforgeeks.org/program-for-array-rotation-continued-reversal-algorithm/
Please write comments if you find any bug in the above programs/algorithms or want to share any additional information about the block swap algorithm.
Please refer complete article on Block swap algorithm for array rotation for more details!
Similar Reads
Javascript Program for Reversal algorithm for array rotation Write a function rotate(arr[], d, n) that rotates arr[] of size n by d elements. Example : Input : arr[] = [1, 2, 3, 4, 5, 6, 7] d = 2 Output : arr[] = [3, 4, 5, 6, 7, 1, 2] Rotation of the above array by 2 will make array Recommended: Please solve it on âPRACTICE â first, before moving on to the so
3 min read
Javascript Program for Reversal algorithm for right rotation of an array Given an array, right rotate it by k elements.  After K=3 rotation Examples: Input: arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} k = 3Output: 8 9 10 1 2 3 4 5 6 7Input: arr[] = {121, 232, 33, 43 ,5} k = 2Output: 43 5 121 232 33Note : In the below solution, k is assumed to be smaller than or equal to n. W
2 min read
Reversal algorithm for Array rotation Given an array arr[] of size N, the task is to rotate the array by d position to the left. Examples: Input: arr[] = {1, 2, 3, 4, 5, 6, 7}, d = 2Output: 3, 4, 5, 6, 7, 1, 2Explanation: If the array is rotated by 1 position to the left, it becomes {2, 3, 4, 5, 6, 7, 1}.When it is rotated further by 1
15 min read
Reversal algorithm for right rotation of an array Given an array, right rotate it by k elements. After K=3 rotation Examples: Input: arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} k = 3 Output: 8 9 10 1 2 3 4 5 6 7 Input: arr[] = {121, 232, 33, 43 ,5} k = 2 Output: 43 5 121 232 33 Note : In the below solution, k is assumed to be smaller than or equal to n
6 min read
Javascript Program for Program to cyclically rotate an array by one Given an array, cyclically rotate the array clockwise by one. Examples: Input: arr[] = {1, 2, 3, 4, 5} Output: arr[] = {5, 1, 2, 3, 4}Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. Following are steps. 1) Store last element in a variable say x. 2) Shift all eleme
2 min read
Javascript Program for Rotate a Matrix by 180 degree Given a square matrix, the task is that we turn it by 180 degrees in an anti-clockwise direction without using any extra space. Examples : Input : 1 2 3 4 5 6 7 8 9Output : 9 8 7 6 5 4 3 2 1Input : 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 Output : 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1Method: 1 (Only prints rotated
6 min read