Block Swap Algorithm
Block Swap Algorithm
Operates by dividing the array into two blocks and then swapping the elements
between these blocks in a specific manner.
APPLICATION:
-> Data shifting: To shift data cyclically such as elements in buffer array or
shift cyclic operations.
-> Memory Management: Can be used to optimize in place array rotation without using
extra memory.
CODE:
class BlockSwap{
static int[] swap(int[] arr, int k, int n){
if(k == 0 | k == n){
return arr;
}
if(k > n){
k %= n;
}
int[] result = new int[n];
for(int i = 0; i < n; i++){
result[i] = arr[(i + k)%n];
//for right rotate (n - k + 1)
}
return result;
}