Q29. Write a code for Left array rotation
Array Rotation:- Array rotation means moving array elements to the left or right of an array by specific position. An array can be moved to the number of given positions by rotating left (clockwise) or right (anti-clockwise). Here we will be discussing left array rotation.
Algorithm For Left array rotation
START
Stop
Code For Left array rotation
#include <stdio.h>
void left_rotation_by_one(int rotate[], int len);
void left_rotate(int rotate[], int times, int len)
{
int i;
for (i = 0; i < times; i++)
left_rotation_by_one(rotate, len);
}
void left_rotation_by_one(int rotate[], int len)
{
int temp = rotate[0], i;
for (i = 0; i < len – 1; i++)
rotate[i] = rotate[i + 1];
rotate[i] = temp;
}
void print_array(int rotate[], int len)
{
int i;
for (i = 0; i < len; i++)
printf(“%d “, rotate[i]);
}
int main()
{
int rotate[] = { 2,5,7,9,11,13,15 };
left_rotate(rotate, 3, 7);
print_array(rotate, 7);
return 0;
}
#include <iostream>
using namespace std;
void left_rotation_by_one(int rotate[], int len)
{
int temp = rotate[0], i;
for (i = 0; i < len – 1; i++)
rotate[i] = rotate[i + 1];
rotate[i] = temp;
}
void left_rotate(int rotate[], int times, int len)
{
for (int i = 0; i < times; i++)
left_rotation_by_one(rotate, len);
}
void print_array(int rotate[], int len)
{
for (int i = 0; i < len; i++)
cout << rotate[i] << ” “;
}
int main()
{
int rotate[] = { 2,5,7,9,11,13,15 };
int len = sizeof(rotate) / sizeof(rotate[0]);
left_rotate(rotate, 3, len);
print_array(rotate, len);
return 0;
}
class ATL {
void left_rotate(int rotate[], int times, int len)
{
for (int i = 0; i < times; i++)
left_rotate_by_one(rotate, len);
}
void left_rotate_by_one(int rotate[], int len)
{
int i, temp;
temp = rotate[0];
for (i = 0; i < len – 1; i++)
rotate[i] = rotate[i + 1];
rotate[i] = temp;
}
void print_array(int rotate[], int len)
{
for (int i = 0; i < len; i++)
System.out.print(rotate[i] + ” “);
}
public static void main(String[] args)
{
ATL rot = new ATL();
int rotate[] = { 2,5,7,9,11,13,15 };
rot.left_rotate(rotate, 3, 7);
rot.print_array(rotate, 7);
}
}
def left_rotate(rotate, times, length):
for i in range(times):
left_rotate_by_ne(rotate, length)
def left_rotate_by_ne(rotate, length):
temp = rotate[0]
for i in range(length-1):
rotate[i] = rotate[i + 1]
rotate[length-1] = temp
def print_array(rotate, length):
for i in range(length):
print (“% d”% rotate[i], end =” “)
rotate = [ 2,5,7,9,11,13,15]
left_rotate(rotate, 3, 7)
print_array(rotate, 7)
function left_rotate_by_one(&$rotate, $len)
{
$temp = $rotate[0];
for ($i = 0; $i < $len – 1; $i++)
$rotate[$i] = $rotate[$i + 1];
$rotate[$i] = $temp;
}
function left_rotate(&$rotate, $times, $len)
{
for ($i = 0; $i < $times; $i++)
left_rotate_by_one($rotate, $len);
}
function print_array(&$rotate, $len)
{
for ($i = 0; $i < $len; $i++)
echo $rotate[$i] . ” “;
}
$rotate = array( 2,5,7,9,11,13,15 );
$len = sizeof($rotate);
left_rotate($rotate, 3, $len);
print_array($rotate, $len);
Output
9,11,13,15,2,5,7
Recommended Programs
Program to find factorial of a number
Program to count number of digits in a number