We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents 3811c9c + b3ecbe1 commit 8b92235Copy full SHA for 8b92235
2D Arrays/sort_matrix_diagonally.py
@@ -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
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