Created
June 26, 2014 23:25
-
-
Save gabhi/16903a8d31c0f7a815f9 to your computer and use it in GitHub Desktop.
Array rotate in place
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ArrayRotate { | |
static void leftRotate(int arr[], int d, int n) { | |
rvereseArray(arr, 0, d - 1); | |
rvereseArray(arr, d, n - 1); | |
rvereseArray(arr, 0, n - 1); | |
} | |
static void rvereseArray(int arr[], int start, int end) { | |
int i; | |
int temp; | |
while (start < end) { | |
temp = arr[start]; | |
arr[start] = arr[end]; | |
arr[end] = temp; | |
start++; | |
end--; | |
} | |
} | |
static void printArray(int arr[], int size) { | |
System.out.println(Arrays.toString(arr)); | |
} | |
public static void main(String[] args) { | |
int arr[] = { 1, 2, 3, 4, 5, 6, 7 }; | |
leftRotate(arr, 2, 7); | |
printArray(arr, 7); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment