sorting a matrix code example

Example: sort matrix

def sortMat(data, row, col):
    size = row * col
    for i in range(0, size):
        for j in range(0, size-1):
            if ( data[j//col][j % col] >\
                data[(j + 1)//col][(j + 1)% col] ):
                temp = data[j//col][j % col]
                data[j//col][j % col] =\
                    data[(j + 1)//col][(j + 1)% col]
                data[(j + 1)//col][(j + 1)% col] =\
                                 temp
  
if __name__ == "__main__":
    mat = [ [5, 4, 7],
            [1, 3, 8],
            [2, 9, 6] ]
    row = len(mat) 
    col = len(mat[0])
      
    # Function call to sort
    sortMat(mat, row, col)
[print(' '.join(i))for i in list(map(lambda i:[str(_) for _ in i],mat))]

Tags:

Misc Example