C Program for Reversal algorithm for array rotation

An algorithm is a set of instructions that are performed in order to solve the given problem. Here, we will discuss the reversal algorithm for array rotation and create a program for a reversal algorithm.

Now, let's get to some terms that we need to know to solve this problem −

Array − A container of elements of the same data type. The size (number of elements) of the array is fixed at the time of the declaration of the array.

Array rotation − Rotating an array is changing the order of the elements of the array. Increasing the index of the element by one and changing the index of the last element to 0 and so on.

Example of array rotation:

Array[] = {3, 6, 8, 1, 4, 10}
Rotated left by 2 positions gives:
Array[] = {8, 1, 4, 10, 3, 6}

Reversal Algorithm

One of the algorithms for array rotation is the reversal algorithm. In this algorithm, subarrays are created and reversed to perform the rotation of the array. The array is split into two parts, both parts are reversed individually, and then the entire array is reversed to get the final rotated result.

Syntax

void reverse(int arr[], int start, int end);
// For left rotation by d positions:
// Step 1: Reverse arr[0...d-1]
// Step 2: Reverse arr[d...n-1]  
// Step 3: Reverse arr[0...n-1]

Algorithm Steps

Input: array arr[], positions to rotate d, array length n
Step 1: Reverse the first d elements (0 to d-1)
Step 2: Reverse the remaining elements (d to n-1)
Step 3: Reverse the entire array (0 to n-1)
Step 4: Print the rotated array

Working Example:

arr[] = {1, 4, 2, 8, 3, 6, 5}, d = 3, n = 7

Step 1: Reverse first 3 elements
        {1, 4, 2} becomes {2, 4, 1}
        arr[] = {2, 4, 1, 8, 3, 6, 5}

Step 2: Reverse remaining elements  
        {8, 3, 6, 5} becomes {5, 6, 3, 8}
        arr[] = {2, 4, 1, 5, 6, 3, 8}

Step 3: Reverse entire array
        arr[] = {8, 3, 6, 5, 1, 4, 2}

Example: Left Rotation Using Reversal Algorithm

#include <stdio.h>

void reverse(int arr[], int start, int end) {
    int temp;
    while (start < end) {
        temp = arr[start];
        arr[start] = arr[end];
        arr[end] = temp;
        start++;
        end--;
    }
}

void rotateLeft(int arr[], int n, int d) {
    // Handle cases where d > n
    d = d % n;
    
    // Step 1: Reverse first d elements
    reverse(arr, 0, d - 1);
    
    // Step 2: Reverse remaining elements
    reverse(arr, d, n - 1);
    
    // Step 3: Reverse entire array
    reverse(arr, 0, n - 1);
}

int main() {
    int arr[] = {54, 67, 12, 76, 25, 16, 34};
    int n = 7;
    int d = 2;
    
    printf("Original array: ");
    for (int i = 0; i < n; i++)
        printf("%d ", arr[i]);
    
    rotateLeft(arr, n, d);
    
    printf("\nArray after left rotation by %d positions: ", d);
    for (int i = 0; i < n; i++)
        printf("%d ", arr[i]);
    printf("
"); return 0; }
Original array: 54 67 12 76 25 16 34 
Array after left rotation by 2 positions: 12 76 25 16 34 54 67

Time and Space Complexity

  • Time Complexity: O(n) − Each element is visited exactly three times during the three reverse operations
  • Space Complexity: O(1) − Only constant extra space is used for the temporary variable

Key Points

  • The reversal algorithm works by using three reverse operations
  • It performs rotation in-place without requiring extra array space
  • The modulo operation (d % n) handles cases where rotation count exceeds array size
  • This algorithm is efficient for both small and large arrays

Conclusion

The reversal algorithm provides an efficient O(n) time and O(1) space solution for array rotation. It elegantly solves the problem using three strategic reverse operations without requiring additional memory allocation.

Updated on: 2026-03-15T12:04:39+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements