×

Block Swap Algorithm for array rotation in Java

An array and r, the rotation factor by which the array must be rotated, are both provided to us. We must then return the rotated array.

A well-known and popular method is Block Swap Algorithm for Array Rotation.

Example

Input
int array [ ]  = {1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ,9 } , n = 9 , r = 3
Output
answer [ ]  = { 4 , 5 , 6 , 7 , 8 ,9 ,1 , 2 , 3 }

Explanation

The input array's size, n, is explained. We must rotate the array three times if r = 3. The first time we rotate, we get the numbers 2, 3, 4, 5, 6, 7, 8, 9, and 1. Now, after rotating the array a second time, we obtain the values 3, 4, 5, 6, 7, 8, 9, 1, and 2. The third rotation is now required. We get {4 , 5 , 6 , 7 , 8 , 9 ,1 , 2 , 3 }

Block Swap Algorithm

Step 1:

First, use div as the division point to split the input array into two smaller arrays. Let A = arr[0... div - 1] and B = arr[div... n - 1] be their respective values.

Step 2:

 Repeat the following procedures up until the sizes of A and B are equal:

Divide A into two sections, A1 and A2, so that the size of A1 is equal to the size of A2, if A is larger than B. Change the A1 and B sub-arrays. The array is altered from A1A2B to BA2A1.

If the size of B is more than the size of A, divide B into B1 and B2, making sure that B2 has the same size as B. Alternate the A and B2 subarrays. The array is changed from AB1B2 to B2B1A.

Step 3: Switch the sizes of A and B when they are equal.

Using Recursion

Program to rotate array using recursion in Java

BlockSwapRecursion.java

public class BlockSwapRecursion 
{ 
public void leftR ( int a [ ] , int p , int q )  
{  
leftRecursive ( a , 0, p , q ) ;  
}  
public void leftRecursive ( int in [ ] , int j , int p , int q )  
{  


if ( p == 0 || p == q )  
{  
return;  
}  
  
if ( q - p == p )  
{  
swap ( in ,  j , q - p + j, p ) ;  
return;  
}  
  
if ( p < q - p )  
{  
swap ( in , j , q - p + j, p ) ;  
leftRecursive ( in , j , p, q - p ) ;      
}  
else 
{  
swap ( in , j , p , q - p ) ;  
leftRecursive ( in , q - p + j , 2 * q - p , p ) ;   
}  
}  


public void swap ( int in [ ] , int f , int g , int r )  
{  
int k ;  
int temp ;  
for ( k = 0 ; k < r ; k++ )  
{  
temp = in [ f + k ] ;  
in [ f + k ] = in [ g + k ] ;  
in [ g + k ] = temp ;  
}  
}  
public static void main (String[] argvs)  
{  
BlockSwapRecursion o1 = new BlockSwapRecursion ();  
int a [ ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ,9 } ;  
int s = a.length;  
int r = 3;  
System . out . println ( " Input array  " ) ;  
for ( int j = 0 ; j < s ; j++ )  
{  
System . out . print ( a [ j ] + " " ) ;  
}  
System . out . println ( ) ;  
System . out . println ( " The new array after rotating " + r + " times , we get " ) ;  
o1 . leftR ( a , r , s ) ;  
for( int k = 0 ; k < s ; k++ )  
{  
System . out . print ( a [ k ] + " " ) ;  
}  
System . out . println ( " \n " ) ;  
// 2nd input  
int a1[] = {76 , 18 , 98 , 59 , 30 , 75 , 0 , 87 , 43 , 77 } ;  
s = a1 . length ;  
r = 5 ;  
System . out . println ( " The input array  " ) ;  
for ( int k = 0 ; k < s ; k++ )  
{  
System . out . print ( a1 [ k ] + " " ) ;  
}  
System . out . println ( ) ;  
System . out . println ( " The new array after rotating " + r + " times , we get " ) ;  
o1 . leftR ( a1 , r , s ) ;  
for ( int k = 0 ; k < s ; k++ )  
{  
System . out . print ( a1 [ k ] + " " ) ;  
}  
}  
}  

Output

Block Swap Algorithm for array rotation in Java

Using Iteration

BlockSwapIteration.java

// Main class of the program
// public class with name 
public class BlockSwapIteration
{  
// The array's components are rotated by r.  
public void left ( int in [ ] , int r , int l )  
{  
int i , j ;  
// if number of time rotating the array is 0 or equal to length of the array return the original array as it is the same
if(r == 0 || r == l)  
{  
// returning original array
return;  
}  
// If the array size is greater than the number of elements that need to be rotated 
if(r > l)  
{  
r = r % l;  
}  
i = r;  
j = l - r;  
// using while loop to iterate through the array
while (i != j)  
{  
if(i < j) 
{  
// using swap functions to swap between the elements of the array
swap ( in, r - i, r + j - i , i ) ;  
j = j - i ;  
}  
else 
{ 
// using swap functions to swap between the elements of the array
swap ( in , r - i , r , j ) ;  
i = i - j ;  
}  
}  
swap(in , r - i , r , i ) ;  
}  
// The procedure replaces the r elements starting at index f with r elements starting at index g.
// swap function to swap elements
public void swap ( int in [ ] , int f , int g , int r )  
{  
// declaring two integer variables k and temp
// k is used to traverse the array
int k ;  
// temp is used in swapping the numbers
int temp ;  
// swap algorithm
for ( k = 0 ; k < r ; k++ )  
{  
temp = in [ f + k ] ;  
in [ f + k ] = in [ g + k ] ;  
in [ g + k ] = temp ;  
}  
}  


// main method where execution of the program starts 
public static void main ( String [ ] args )  
{  
// creating an object for the Main class 
BlockSwapIteration o1 = new BlockSwapIteration ( ) ;  
// 1 st input  
int a [ ] = { 17 , 29 , 85 , 10 , 45 , 71 , 26 , 48 } ;  
// finding the length of the array using length predefined method
int l = a . length ;  
// number of times the array need to be rotated
System . out . println (  " Number of times array need to be rotated ") ;
int r = 3 ;  
System . out . println ( " The input array: " ) ;  
// printing the input array
for ( int i = 0 ; i < l ; i++ )  
{  
System . out . print ( a [ i ] + " " ) ;  
}  
System . out . println ( ) ;  
// printing the modifid rotated array
System . out . println ( " New array after rotating " + r + " time " ) ;  
// calling the left method using object created
o1 . left ( a, r, l ) ;  
for ( int i = 0 ; i < l ; i++ )  
{  
System . out . print ( a [ i ] + " "  ) ;  
}  
System . out . println ( " \n " ) ;  
// 2 nd input  
int a1 [ ] = { 26 , 18 , 27 , 39 , 10 , 25 , 51 , 63 , 92 , 34 } ;  
l = a1 . length ;  
r = 4 ;  
System . out . println ( " Input array: " ) ;  
for ( int i = 0 ; i < l ; i++ )  
{  
System . out . print ( a1 [ i ] + " " ) ;  
}   
System . out . println ( ) ;   
System . out . println ( " New array after rotating " + r + " times " ) ;  
o1 . left (a1, r, l) ;  
for ( int i = 0 ; i < l ; i++ )  
{  
System . out . print ( a1 [ i ] + " " ) ;  
}   
}  
}

Output:

Block Swap Algorithm for array rotation in Java

Related Topics

Binary Search Java

Binary search is a search mechanism for key elements from the given List/Array. In Binary search, the search mechanism is followed by dividing the array into parts; hence the search...

3 minutes read.

Repdigit Numbers in Java

A combination of repeated and digit, the term is. A repdigit, also known as a monodigit or a positional number system, is a natural number in recreational mathematics made up...

3 minutes read.

What is anagram in Java?

In this section will explain what an anagram is in Java and demonstrate how to determine whether or not a text is an anagram. In Java interviews, the anagram Java...

4 minutes read.

How to Print array in Java?

A Java array is a data structure that allows us to hold components of the same data type. An array's items are kept in a single memory region. As a...

6 minutes read.

Read the XLSX file in Java

Excel files contain cells; reading an excel file in Java differs from reading a word file. JDK doesn't have a direct API that can read or write Word or Excel...

3 minutes read.

Counting sort in Java

An array's elements are sorted using the counting sort method, which counts how many times each distinct element appears in the array. The count is kept in an auxiliary array,...

3 minutes read.

How to Convert Timestamp to Date in Java

How to Convert Timestamp to Date in Java You can convert Timestamp to Date by using the constructor of Date class. It returns the long millisecond from Epoch (1st January 1970)...

2 minutes read.

Java FileNotFoundException

FileNotFoundException is another exception class accessible in the java.io bundle. The exemption happens when we attempt to get to that document which isn't accessible in the framework. It is a checked...

5 minutes read.

Add Time in Java

Before Java 8, java.util.Date was one of the most usually involved classes for addressing date-time values in java. Then Java 8 presented java.time.LocalDateTime and java.time.ZonedDateTime. Java 8 likewise permits us...

6 minutes read.

Program to find and replace characters on string in java

In JAVA, Strings are immutable. To overcome this Java String class contains a lot of methods to do operations on Strings. One such method is replace method. String Replace It returns a...

3 minutes read.

Java vs Node.js

Java: Java is an object oriented programming language. It is also known as multi threaded language. It was designed by James gosling in the year 1995. We can also say that...

4 minutes read.

Java Arrays Fill

We may use the Arrays.fill () function to fill a whole array or a subset of it. Arrays.fill () may fill both 2D and 3D arrays. Syntax: Arrays.fill(boolean[] fillArr, int fromIndex, int toIndex, boolean val )   Parameters: The array to be filled...

4 minutes read.

Java Integer getInteger() method

The getInteger() method of Integer class determines the integer value of the system property with the given name. Syntax` public static Integer getInteger(String nm) Parameters The parameter ‘nm’ represents the property name. Throws The getInteger ()...

1 minute read.

How to set timer in Java

In this article, you will be very well equipped with the knowledge to set timer in java. The timer in java can be set by using timer class provided by...

3 minutes read.

Java String getChars() Method

Java String getChars() method copies characters from current String to the destination character array . Syntax: public void getChars(int srcBeginIndex, int srcEndIndex, char[] destination, int dstBeginIndex) Parameters: srcBegin - index of the first character...

1 minute read.

Java String

Definition A string is a series of characters. Consider an example, "Welcome" is a 7-character string. In Java, String is an unchangeable object. That is, the String is perpetual and cannot be replaced after it is created. This is the tutorial in which you...

4 minutes read.

Generic Linked List in Java

A linear data structure known as a Linked List stores values in nodes. As we already know, each node has two properties: its value and a link to the node...

6 minutes read.

Java File

Java file class implements the concept of file handling. It has several methods, such as deleting, creating, reading, and updating files. This class allows java users to perform various operations...

5 minutes read.

Getting the Day from the Date in Java?

To extract the day's name from the date, we will write Java application. When dealing the date and time in Java, the following classes are used. Class for Calendars: This class is...

6 minutes read.

Convert IP to Binary in Java

Fundamental conversion, such as going from binary to decimal or vice versa, is a crucial activity in computers. Understanding IP addressing and subnetting is crucial for networking. The primary networking...

4 minutes read.