Skip to content

Commit 8b92235

Browse files
authored
Merge pull request #616 from Adarsh88/master
Added matrix files
2 parents 3811c9c + b3ecbe1 commit 8b92235

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

2D Arrays/sort_matrix_diagonally.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
def sortDiagonal(a, M, N):
2+
3+
# Loop to find the ith minimum
4+
# element from the major diagonally
5+
for i in range(M):
6+
sm = a[i][i]
7+
pos = i
8+
9+
# Loop to find the minimum
10+
# element from the unsorted matrix
11+
for j in range(i + 1 , N):
12+
if (sm > a[j][j]):
13+
sm = a[j][j]
14+
pos = j
15+
16+
# Swap to put the minimum
17+
# element at the beginning of
18+
# the major diagonal of matrix
19+
a[i][i], a[pos][pos] = a[pos][pos] , a[i][i]
20+
21+
# Loop to print the matrix
22+
for i in range(M):
23+
for j in range(N):
24+
print(a[i][j],end=" ")
25+
print()
26+
27+
# Driven Code
28+
a = [[4, 2],[3, 1]]
29+
30+
# Sort the major Diagonally
31+
sortDiagonal(a, 2, 2)

0 commit comments

Comments
 (0)