Skip to content

Instantly share code, notes, and snippets.

@kjunine
Created September 1, 2012 11:00
Show Gist options
  • Select an option

  • Save kjunine/3569733 to your computer and use it in GitHub Desktop.

Select an option

Save kjunine/3569733 to your computer and use it in GitHub Desktop.
Rotate array without copy
This is a result.
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 2, 8, 3, 4, 5, 6, 7, 9]
[1, 2, 7, 8, 3, 4, 5, 6, 9]
[1, 2, 6, 7, 8, 3, 4, 5, 9]
[1, 2, 4, 5, 6, 7, 8, 3, 9]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 2, 8, 3, 4, 5, 6, 7, 9]
rotateArray: 862 ms
rotateArrayUsingCopy: 712 ms
This is a result when the array size of performance test is 300 * 1024 * 1024.
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 2, 8, 3, 4, 5, 6, 7, 9]
[1, 2, 7, 8, 3, 4, 5, 6, 9]
[1, 2, 6, 7, 8, 3, 4, 5, 9]
[1, 2, 4, 5, 6, 7, 8, 3, 9]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 2, 8, 3, 4, 5, 6, 7, 9]
rotateArray: 2526 ms
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOfRange(Arrays.java:3137)
at net.kjunine.insightquiz.InsightQuiz01.rotateArrayUsingCopy(InsightQuiz01.java:37)
at net.kjunine.insightquiz.InsightQuiz01.main(InsightQuiz01.java:84)
package net.kjunine.insightquiz;
import java.util.Arrays;
public class InsightQuiz01 {
public static void rotateArray(int[] array, int s, int t, int k) {
int start = s;
int length = t - s + 1;
int offset = k;
int remain = k % length;
if (remain == 0) {
return;
} else {
int current = 0;
int target = 0;
int item = array[start + current];
for (int i = 0; i < length; i++) {
current = (current + offset) % length;
int temp = array[start + current];
array[start + current] = item;
item = temp;
if (current == target) {
current++;
target++;
item = array[start + current];
}
}
}
}
public static void rotateArrayUsingCopy(int[] array, int s, int t, int k) {
int start = s;
int end = t + 1;
int length = t - s + 1;
int offset = k;
int[] copied = Arrays.copyOfRange(array, start, end);
for (int i = 0; i < length; i++) {
array[start + (i + offset) % length] = copied[i];
}
}
public static void main(String[] args) {
int[] array = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
System.out.println(Arrays.toString(array));
rotateArray(array, 2, 7, 1);
System.out.println(Arrays.toString(array));
array = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
rotateArray(array, 2, 7, 2);
System.out.println(Arrays.toString(array));
array = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
rotateArray(array, 2, 7, 3);
System.out.println(Arrays.toString(array));
array = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
rotateArray(array, 2, 7, 5);
System.out.println(Arrays.toString(array));
array = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
rotateArray(array, 2, 7, 6);
System.out.println(Arrays.toString(array));
array = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
rotateArray(array, 2, 7, 7);
System.out.println(Arrays.toString(array));
// performance
array = new int[100 * 1024 * 1024];
for (int i = 0; i < array.length; i++) {
array[i] = i;
}
long start = System.currentTimeMillis();
rotateArray(array, 0, array.length - 1, 5);
long time = System.currentTimeMillis() - start;
System.out.println("rotateArray: " + time + " ms");
array = new int[100 * 1024 * 1024];
for (int i = 0; i < array.length; i++) {
array[i] = i;
}
start = System.currentTimeMillis();
rotateArrayUsingCopy(array, 0, array.length - 1, 5);
time = System.currentTimeMillis() - start;
System.out.println("rotateArrayUsingCopy: " + time + " ms");
}
}
@kjunine
Copy link
Author

kjunine commented Sep 1, 2012

Without-copy version is a little bit slower but uses memory lesser.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment