-
Notifications
You must be signed in to change notification settings - Fork 0
/
BlockSwapAlg.java
34 lines (29 loc) · 903 Bytes
/
BlockSwapAlg.java
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
class BlockSwapAlg{
static int arr[]={1,2,3,4,5,6,7};
public static void swap( int r, int first, int sec){
for(int i = 0 ; i < r ; i++){
int temp = arr[first + i];
arr[first + i] = arr[sec + i];
arr[sec + i] = temp;
}
}
public static void main(String[] args){
int n = arr.length, r = 4, i = r, j = n - r;
if(r!=0 && r!=n){
while (i != j){
if(i < j){
swap( i, r-i, r+j-i);
j = j - i;
}
else{
swap(j, r-i, r);
i = i - j;
}
}
swap( i, r-i, r);
}
System.out.println("Array Elements after rotating : ");
for(int k = 0 ; k < n ; k++)
System.out.print(arr[k] + " ");
}
}