-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRotateArray.java
More file actions
36 lines (29 loc) · 765 Bytes
/
RotateArray.java
File metadata and controls
36 lines (29 loc) · 765 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package Arrays;
import java.util.Arrays;
public class RotateArray {
public static void main(String[] args) {
RotateArray ra=new RotateArray();
int[] ltArray={1,2,3,4,5,6};
ra.leftRotate(ltArray, 1);
System.out.println(Arrays.toString(ltArray));
int[] rtArray={1,2,3,4,5,6};
ra.rightRotate(rtArray, 1);
System.out.println(Arrays.toString(rtArray));
}
public void leftRotate(int[] ltArray,int shift){
while(shift-->0){
int i,temp=ltArray[0];
for(i=0;i<ltArray.length-1;i++)
ltArray[i]=ltArray[i+1];
ltArray[i]=temp;
}
}
public void rightRotate(int[] rtArray,int shift){
while(shift-->0){
int i,temp=rtArray[rtArray.length-1];
for(i=rtArray.length-1;i>0;i--)
rtArray[i]=rtArray[i-1];
rtArray[i]=temp;
}
}
}