Created
June 23, 2014 05:51
-
-
Save gabhi/a2ba9bdb52fe02226507 to your computer and use it in GitHub Desktop.
matrix rotation 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
void rotateRight(int matrix[][SIZE], int length) { | |
int layer = 0; | |
for (int layer = 0; layer < length / 2; ++layer) { | |
int first = layer; | |
int last = length - 1 - layer; | |
for (int i = first; i < last; ++i) { | |
int topline = matrix[first][i]; | |
int rightcol = matrix[i][last]; | |
int bottomline = matrix[last][length - layer - 1 - i]; | |
int leftcol = matrix[length - layer - 1 - i][first]; | |
matrix[first][i] = leftcol; | |
matrix[i][last] = topline; | |
matrix[last][length - layer - 1 - i] = rightcol; | |
matrix[length - layer - 1 - i][first] = bottomline; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment