Skip to content

Commit

Permalink
added array rotation 3 approaches
Browse files Browse the repository at this point in the history
  • Loading branch information
GauravWalia19 committed Apr 18, 2019
1 parent e64c51f commit a3536b9
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 68 deletions.
42 changes: 42 additions & 0 deletions Java/Data-Structures/ARRAYS/MISC/ArrayRotation1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* ARRAY ROTATION USING TEMP ARRAY
* LEFT ROTATE TO 2
*/
import java.util.*;
public class ArrayRotation1
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int[] arr = {1,2,3,4,5,6,7};
int shift=2;

rotate(arr,shift,arr.length);

in.close();
}
public static void rotate(int[] arr,int d,int n)
{
int[] temp = new int[d];
//copy arr 2 elements to temp
for(int i=0;i<temp.length;i++)
{
temp[i] = arr[i];
}
//shifting array
for(int i=0;i<arr.length-d;i++)
{
arr[i]=arr[i+d];
}
//copy temp array to array
for(int i=0;i<d;i++)
{
arr[i+arr.length-d] =temp[i];
}
for(int i=0;i<arr.length;i++)
{
System.out.print(arr[i]+" ");
}
System.out.println();
}
}
35 changes: 35 additions & 0 deletions Java/Data-Structures/ARRAYS/MISC/ArrayRotation2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* ARRAY ROTATION USING ONE ELEMENT
* LEFT ROTATE BY 2
*/
import java.util.*;
public class ArrayRotation2
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int[] arr={1,2,3,4,5,6,7};
int shift=3;
rotate(arr, shift, arr.length);

//printing array
for(int i=0;i<arr.length;i++)
{
System.out.print(arr[i]+" ");
}
in.close();
}
public static void rotate(int[] arr,int d,int n)
{
//we can shift one by one
for(int i=0;i<d;i++)
{
int temp = arr[0];
for(int j=0;j<n-1;j++)
{
arr[j]=arr[j+1];
}
arr[n-1]=temp;
}
}
}
44 changes: 44 additions & 0 deletions Java/Data-Structures/ARRAYS/MISC/jugglingAlgo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
abstract class jugglingAlgo
{
public static void main(String[] args)
{
int[] arr = {1,2,3,4,5,6,7,8,9};
leftRotation(arr,3);
for(int i=0;i<arr.length;i++)
{
System.out.print(arr[i] + " ");
}
System.out.println();
}
public static int[] leftRotation(int[] arr,int k)
{
int set = gcd(arr.length, k);

for(int i=0;i<set;i++)
{
int temp = arr[i];
int j=i;
while(true)
{
int d = (j+k) % arr.length;

if(d==i)
{
break;
}
arr[j] = arr[d];
j=d;
}
arr[j] = temp;
}
return arr;
}
public static int gcd(int a,int b)
{
if(b==0)
{
return a;
}
return gcd(b,a%b);
}
}
4 changes: 4 additions & 0 deletions Java/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
* MISC
* JAGGED ARRAY
* [SPIRAL ORDER MATRIX](Data-Structures/ARRAYS/MISC/spiralOrder.java)
* Rotate 1D array to left by K
* APPROACH 1: [rotate array using temp array](Data-Structures/ARRAYS/MISC/ArrayRotation1.java)
* APPROACH 2: [rotate array by rotating one element](Data-Structures/ARRAYS/MISC/ArrayRotation2.java)
* APPROACH 3: [rotate array using Juggling Algorithm](Data-Structures/ARRAYS/MISC/jugglingAlgo.java)

#### STRING

Expand Down
Loading

0 comments on commit a3536b9

Please sign in to comment.