Skip to content

Instantly share code, notes, and snippets.

@deximat
Created February 13, 2016 12:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deximat/f3bbcb2e36058a6ca0db to your computer and use it in GitHub Desktop.
Save deximat/f3bbcb2e36058a6ca0db to your computer and use it in GitHub Desktop.
private static void rotate(int[] array, int r) {
reverse(array, 0, array.length);
reverse(array, 0, r);
reverse(array, r + 1, array.length);
}
private static void reverse(int[] array, int start, int end) {
while (start < end) {
swap(array, start, end);
start++;
end--;
}
}
private static void swap(int[] array, int start, int end) {
int tmp = array[start];
array[start] = array[end];
array[end] = tmp;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment