Skip to content

Commit 0ec9d97

Browse files
Initial Commit
1 parent 5412fdf commit 0ec9d97

1 file changed

Lines changed: 99 additions & 0 deletions

File tree

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package easyq;
2+
3+
/* BY-M.NAVEEN
4+
* engineerscodes
5+
* code -Program for array rotation
6+
* sr.code-Ar3
7+
* moves zeros to end of the array
8+
* */
9+
import java.util.*;
10+
public class Program_for_array_rotation //class name
11+
{
12+
static Scanner nav=new Scanner(System.in);
13+
static int array[],tem[],temr[],rotation_d; // class instances variables
14+
15+
public Program_for_array_rotation(int size)
16+
{
17+
array=new int[size]; //intialization of variables
18+
19+
}
20+
public static void lrotation() // method for leftrotation
21+
{
22+
for(int i=0;i<rotation_d;i++)
23+
{tem[i]=array[i]; // copy the beginning elements from 0 -rotation_d(number of element to rotate)
24+
System.out.println("temp rotatio _="+Arrays.toString(tem));
25+
}
26+
leftshift();// calling leftshift to move the elemts to beginining of the array
27+
}
28+
public static void rrotation() // method for rightrotation
29+
{
30+
for(int i=array.length-rotation_d,j=0;i<array.length;i++,j++)
31+
{ temr[j]=array[i]; // copy the beginning elements from 0 -rotation_d(number of element to rotate)
32+
System.out.println("temp rotatio _righ//t="+Arrays.toString(temr));
33+
}
34+
rightshift();// calling right to move the elemts to beginining of the array
35+
36+
}
37+
public static void leftshift() //shifting the elements
38+
{ int count=1;
39+
for(int i=0;i<array.length-rotation_d;i++) // for loop to traverse across array
40+
{
41+
array[i]=array[i+rotation_d]; //shifting
42+
43+
}
44+
for(int i=array.length-1,j=tem.length-1;count<=rotation_d;i--,j--) //assigning temp array values to array back
45+
{
46+
array[i]=tem[j];
47+
count++;
48+
}
49+
System.out.println("After rotatio _="+Arrays.toString(array));
50+
}
51+
public static void rightshift() //shifting the elements
52+
{ int j=array.length-1;
53+
for(int i=array.length-1-rotation_d;i>=0;i--) // for loop to traverse across array
54+
{
55+
array[j]=array[i];//shifting
56+
j--;
57+
}
58+
for(int i=0;i<rotation_d;i++) //assigning temp array values to array back
59+
{
60+
array[i]=temr[i];
61+
62+
}
63+
System.out.println("After rotatio right="+Arrays.toString(array));
64+
}
65+
public static void input() //input function
66+
{
67+
for(int i=0;i<array.length;i++)
68+
array[i]=nav.nextInt();
69+
System.out.println("element in array are::"+Arrays.toString(array));
70+
System.out.println("enter the rotationby");
71+
rotation_d=nav.nextInt();
72+
rotation_d=rotation_d%array.length;
73+
tem=new int[rotation_d];
74+
temr=new int[rotation_d];
75+
}
76+
77+
public static void main(String arg[]) //main method
78+
{
79+
System.out.println("enter the size of the array");
80+
Program_for_array_rotation n=new Program_for_array_rotation(nav.nextInt());
81+
System.out.println("Enter the element of array");
82+
input();
83+
while (true)
84+
{
85+
System.out.println("enter 1)\nleft shift or 2)\nrigth shift 3>\nexit");
86+
switch(nav.nextInt())
87+
{
88+
case 1:lrotation();
89+
break;
90+
case 2: rrotation();
91+
break;
92+
case 3: System.exit(0);
93+
}
94+
95+
}
96+
97+
98+
}
99+
}

0 commit comments

Comments
 (0)