forked from avani112/LearnPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrix_sort.py
More file actions
49 lines (34 loc) · 735 Bytes
/
matrix_sort.py
File metadata and controls
49 lines (34 loc) · 735 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# Python3 program to sort a given matrix
SIZE = 10
def sortMat(mat, n) :
# Temporary matrix of size n^2
temp = [0] * (n * n)
k = 0
# Copy the elements of the matrix into temp[]
for i in range(0, n) :
for j in range(0, n) :
temp[k] = mat[i][j]
k += 1
# sort temp[]
temp.sort()
# copy the elements of temp[] into mat[][]
k = 0
for i in range(0, n) :
for j in range(0, n) :
mat[i][j] = temp[k]
k += 1
def printMat(mat, n) :
for i in range(0, n) :
for j in range( 0, n ) :
print(mat[i][j] , end = " ")
print()
# Test Case
mat = [ [ 5, 4, 7 ],
[ 1, 3, 8 ],
[ 2, 9, 6 ] ]
n = 3
print( "Given Matrix:")
printMat(mat, n)
sortMat(mat, n)
print("\nSorted Matrix: ")
printMat(mat, n)