Skip to content

Commit

Permalink
added matrix rotation 90degrees (jainaman224#934)
Browse files Browse the repository at this point in the history
  • Loading branch information
KanchanThareja authored and jainaman224 committed Apr 14, 2019
1 parent 8aaba2b commit 840f984
Show file tree
Hide file tree
Showing 4 changed files with 188 additions and 0 deletions.
47 changes: 47 additions & 0 deletions Matrix_Operations/Matrix_Rotation90.c
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
*/
47 changes: 47 additions & 0 deletions Matrix_Operations/Matrix_Rotation90.cpp
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
*/
50 changes: 50 additions & 0 deletions Matrix_Operations/Matrix_Rotation90.java
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
*/
44 changes: 44 additions & 0 deletions Matrix_Operations/Matrix_Rotation90.py
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
'''

0 comments on commit 840f984

Please sign in to comment.