-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d65cdcf
commit 4557738
Showing
1 changed file
with
50 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,50 @@ | ||
def sortDiagonal(a, M, N): | ||
# Loop to find the ith minimum | ||
# element from the major diagonally | ||
for i in range(M): | ||
sm = a[i][i] | ||
pos = i | ||
|
||
# Loop to find the minimum | ||
# element from the unsorted matrix | ||
for j in range(i+1, N): | ||
if sm >= a[j][j]: | ||
sm = a[j][j] | ||
pos = j | ||
|
||
# Swap to put the minimum | ||
# element at the beginning of | ||
# the major diagonal of matrix | ||
a[i][i], a[pos][pos] = a[pos][pos], a[i][i] | ||
|
||
# Loop to print the matrix | ||
for i in range(M): | ||
for j in range(N): | ||
print(a[i][j], end=" ") | ||
print() | ||
|
||
|
||
''' | ||
It loops through the major diagonal of the matrix, from the top left to the bottom right (line 6-8). | ||
For each element on the major diagonal, it finds the minimum element from the remaining unsorted elements in the diagonal (lines 10-14). | ||
If a smaller element is found, it swaps the current element with the minimum element to put the minimum element at the beginning of the major diagonal (line 15). | ||
Finally, the function prints the sorted matrix, with the major diagonal elements sorted in ascending order (lines 18-21). | ||
''' | ||
|
||
a = [[4, 2, 5], [3, 1, 5], [9, 8, 4]] | ||
sortDiagonal(a, 3, 3) | ||
|
||
#b = [[7, 2, 3], [3, 1, 6], [9, 8, 4]] | ||
#sortDiagonal(b, 3, 3) | ||
|
||
''' | ||
[[4, 2, 5], [3, 1, 6], [9, 8, 7]] | ||
1 2 5 | ||
3 4 6 | ||
9 8 7 | ||
[[4, 2, 5], [3, 1, 5], [9, 8, 4]] | ||
1 2 5 | ||
3 4 5 | ||
9 8 4 | ||
''' |