A cyclic rotation of an array in C shifts every element one position to the left or right, with the element that falls off one end wrapping around to the other. A left rotation moves the first element to the last position; a right rotation moves the last element to the first position. This operation appears in scheduling algorithms, circular buffer implementations, and data transformation pipelines where elements cycle through positions.
How It Works — Step by Step
Given the array [10, 20, 30, 40, 50]:
Left rotation — first element wraps to the end:
- Save
arr[0] = 10(it will be overwritten). - Shift every element one position left: arr[0]=20, arr[1]=30, arr[2]=40, arr[3]=50.
- Place the saved value at the last position: arr[4]=10.
- Result: [20, 30, 40, 50, 10]
Right rotation — last element wraps to the front:
- Save
arr[4] = 50. - Shift every element one position right: arr[4]=40, arr[3]=30, arr[2]=20, arr[1]=10.
- Place the saved value at position 0: arr[0]=50.
- Result: [50, 10, 20, 30, 40]
C Program for Cyclic Rotation of an Array
/* Cyclic left and right rotation of an array in C
* Compile: gcc -ansi -Wall -Wextra cyclic.c -o cyclic */
#include <stdio.h>
#define MAX 100
void print_array(int arr[], int n)
{
int i;
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}
void rotate_left(int arr[], int n, int copy[])
{
int i;
copy[n - 1] = arr[0];
for (i = 0; i < n - 1; i++)
copy[i] = arr[i + 1];
}
void rotate_right(int arr[], int n, int copy[])
{
int i;
copy[0] = arr[n - 1];
for (i = 1; i < n; i++)
copy[i] = arr[i - 1];
}
int main(void)
{
int arr[MAX], left[MAX], right[MAX], n, i;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter %d elements:\n", n);
for (i = 0; i < n; i++)
scanf("%d", &arr[i]);
rotate_left(arr, n, left);
rotate_right(arr, n, right);
printf("Original: ");
print_array(arr, n);
printf("After left rotate: ");
print_array(left, n);
printf("After right rotate: ");
print_array(right, n);
return 0;
}
How to Compile and Run
gcc -ansi -Wall -Wextra cyclic.c -o cyclic
./cyclic
Sample Input and Output
Test 1:
Enter number of elements: 5 Enter 5 elements: 10 20 30 40 50 Original: 10 20 30 40 50 After left rotate: 20 30 40 50 10 After right rotate: 50 10 20 30 40
Test 2:
Enter number of elements: 4 Enter 4 elements: 1 2 3 4 Original: 1 2 3 4 After left rotate: 2 3 4 1 After right rotate: 4 1 2 3
Code Explanation
- copy[] arrays — both
rotate_left()androtate_right()write into a separate output array, leaving the original untouched. This lets both rotations be computed from the same input in one pass. - rotate_left logic — assigns
arr[0]tocopy[n-1]first to avoid losing it, then copiesarr[1..n-1]intocopy[0..n-2]. - rotate_right logic — assigns
arr[n-1]tocopy[0]first, then copiesarr[0..n-2]intocopy[1..n-1]. - #define MAX 100 — a named constant avoids VLAs (variable-length arrays), which are optional in C11 and absent in C89/C90. The program compiles cleanly under
-ansi.
Time and Space Complexity
| Operation | Time | Space |
|---|---|---|
| Single rotation (left or right) | O(n) | O(n) — copy array |
| In-place single rotation | O(n) | O(1) — one saved element |
| k rotations (naive) | O(k × n) | O(1) |
What This Program Teaches
- Array wrap-around with a saved element — saving the element that would be overwritten before shifting is the key pattern in all rotation and reverse algorithms.
- Read from one array, write to another — avoids aliasing bugs when computing two results from the same source in a single pass.
- Arrays as function parameters — arrays in C decay to pointers when passed to functions;
int arr[]andint *arrare equivalent in a parameter list. - Named constants with #define — using
MAXinstead of a magic number makes the bound easy to change and self-documenting.
Related Programs
- Insert an Element into an Array in C
- Delete an Element from an Array in C
- Array Search in C
- Bubble Sort in C
- Merge Two Arrays in C
Recommended book:
The C Programming Language — Kernighan & Ritchie (India) |
(US)
|
C Programming: A Modern Approach — K.N. King (India) |
(US)
Practice what you learned: C Aptitude Questions — or try our C Programming Quiz App on Android.