forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added matrix rotation 90degrees (jainaman224#934)
- Loading branch information
1 parent
8aaba2b
commit 840f984
Showing
4 changed files
with
188 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#include <bits/stdc++.h> | ||
#define N 3 | ||
using namespace std; | ||
|
||
void Rotate(int matrix[][N]) | ||
{ | ||
for (int x = 0; x < N / 2; x++) | ||
{ | ||
for (int i = x; i < N-x-1; i++) | ||
{ | ||
int t = matrix[x][i]; | ||
matrix[x][i] = matrix[i][N-1-x]; | ||
matrix[i][N-1-x] = matrix[N-1-x][N-1-i]; | ||
matrix[N-1-x][N-1-i] = matrix[N-1-i][x]; | ||
matrix[N-1-i][x] = t; | ||
} | ||
} | ||
} | ||
|
||
//Driver program | ||
int main() | ||
{ | ||
// Sample Input | ||
int matrix[N][N] = | ||
{ | ||
{1, 2, 3}, | ||
{5, 6, 7}, | ||
{9, 10, 11} | ||
}; | ||
Rotate(matrix); | ||
|
||
for (int i = 0; i < N; i++) | ||
{ | ||
for (int j = 0; j < N; j++) | ||
printf("%2d ", mat[i][j]); | ||
|
||
printf("\n"); | ||
} | ||
printf("\n"); | ||
return 0; | ||
} | ||
|
||
/* Sample Output: | ||
3 7 11 | ||
2 6 10 | ||
1 5 9 | ||
*/ |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#include <bits/stdc++.h> | ||
#define N 3 | ||
using namespace std; | ||
|
||
void Rotate(int matrix[][N]) | ||
{ | ||
for (int x = 0; x < N / 2; x++) | ||
{ | ||
for (int i = x; i < N-x-1; i++) | ||
{ | ||
int t = matrix[x][i]; | ||
matrix[x][i] = matrix[i][N-1-x]; | ||
matrix[i][N-1-x] = matrix[N-1-x][N-1-i]; | ||
matrix[N-1-x][N-1-i] = matrix[N-1-i][x]; | ||
matrix[N-1-i][x] = t; | ||
} | ||
} | ||
} | ||
|
||
//Driver program | ||
int main() | ||
{ | ||
// Sample Input | ||
int matrix[N][N] = | ||
{ | ||
{1, 2, 3}, | ||
{5, 6, 7}, | ||
{9, 10, 11} | ||
}; | ||
Rotate(matrix); | ||
|
||
for (int i = 0; i < N; i++) | ||
{ | ||
for (int j = 0; j < N; j++) | ||
cout << matrix[i][j]<<"\t"; | ||
|
||
cout << endl; | ||
} | ||
cout << endl; | ||
return 0; | ||
} | ||
|
||
/* Sample Output: | ||
3 7 11 | ||
2 6 10 | ||
1 5 9 | ||
*/ |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import java.io.*; | ||
|
||
class matrixRotation | ||
{ | ||
static void Rotate(int matrix[][], int N) | ||
{ | ||
for (int i = 0; i < N / 2; i++) | ||
{ | ||
for (int j = i; j < N-i-1; j++) | ||
{ | ||
int t = matrix[i][j]; | ||
matrix[i][j] = matrix[j][N-1-i]; | ||
matrix[j][N-1-i] = matrix[N-1-i][N-1-j]; | ||
matrix[N-1-i][N-1-j] = matrix[N-1-j][i]; | ||
matrix[N-1-j][i] = t; | ||
} | ||
} | ||
} | ||
|
||
public static void main (String[] args) | ||
{ | ||
int N = 3; | ||
|
||
// Sample Input | ||
int matrix[][] = | ||
{ | ||
{1, 2, 3}, | ||
{5, 6, 7}, | ||
{9, 10, 11} | ||
}; | ||
Rotate(matrix, N); | ||
|
||
// Print rotated matrix | ||
for (int i = 0; i < N; i++) | ||
{ | ||
for (int j = 0; j < N; j++) | ||
System.out.print(" " + matrix[i][j]); | ||
|
||
System.out.print("\n"); | ||
} | ||
System.out.print("\n"); | ||
} | ||
} | ||
|
||
/* Sample Output: | ||
3 7 11 | ||
2 6 10 | ||
1 5 9 | ||
*/ |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
N = 3 | ||
|
||
# A function to rotate N x N matrix by 90 degrees in anti-clockwise direction | ||
def Rotate(Matrix): | ||
|
||
a = int(N/2) | ||
for x in range(0, a): | ||
|
||
# elements in group of 4 in square | ||
for y in range(x, N-x-1): | ||
|
||
t = Matrix[x][y] | ||
Matrix[x][y] = Matrix[y][N-1-x] | ||
Matrix[y][N-1-x] = Matrix[N-1-x][N-1-y] | ||
Matrix[N-1-x][N-1-y] = Matrix[N-1-y][x] | ||
Matrix[N-1-y][x] = t | ||
|
||
# Sample Input | ||
Matrix = [ [1, 2, 3], [5, 6, 7], [9, 10, 11 ] ] | ||
|
||
Rotate(Matrix) | ||
|
||
for i in range(0, N): | ||
|
||
for j in range(0, N): | ||
print (Matrix[i][j]) | ||
|
||
print ("") | ||
|
||
''' | ||
Sample output : | ||
3 | ||
7 | ||
11 | ||
2 | ||
6 | ||
10 | ||
1 | ||
5 | ||
9 | ||
''' |