Python 按行和按列对矩阵进行排序

Python 按行和按列对矩阵进行排序

在本文中,我们将学习使用Python编写程序来按行和按列对矩阵进行排序。

假设我们输入了一个 MxM 矩阵。我们将使用嵌套循环按行和按列对给定的输入矩阵进行排序。

步骤

以下是要执行所需任务的算法/步骤:

  • 创建一个函数 sortingMatrixByRow() 通过接受输入矩阵m(行数)作为参数来对矩阵的每一行进行排序,即按行进行排序。

  • 在函数内部,使用 for循环 遍历矩阵的行。

  • 使用另外一个嵌套的 for循环 遍历当前行的所有列。

  • 使用条件语句 if 检查当前元素是否大于下一个元素。

  • 如果条件为 true ,使用临时变量交换元素。

  • 创建另一个函数 transposeMatrix() 通过接受输入矩阵m来获取矩阵的转置。

  • 使用 for循环 遍历矩阵的行。

  • 使用另一个 嵌套for循环 遍历从当前行+1到最后一列的列。

  • 将当前行列元素与列行元素进行交换。

  • 创建一个名为 sortMatrixRowandColumn() 的函数,通过输入矩阵和行数作为参数,按行和列方式对矩阵进行排序。

  • 在函数内部,调用上面定义的 sortingMatrixByRow() 函数对输入矩阵的行进行排序。

  • 调用上面定义的 transposeMatrix() 函数

实现一个函数以获取输入矩阵的转置。

  • 通过调用上述定义的 sortingMatrixByRow() 函数,再次对输入矩阵的行进行排序。

  • 通过调用上述定义的 transposeMatrix() 函数,再次获取输入矩阵的转置。

  • 创建一个名为 printingMatrix() 的函数,通过嵌套的for循环遍历矩阵的行和列来打印矩阵。

  • 创建一个变量来存储 输入矩阵

  • 创建另一个变量来存储输入 m(行数) 的值。

  • 调用上述定义的 printingMatrix() 函数。
    “`javascript function to print the input matrix.

  • 调用上述定义的 sortMatrixRowandColumn() 函数,通过向其传递输入矩阵和m值来按行和按列对矩阵进行排序。

  • 通过调用上述定义的 printingMatrix() 函数,打印排序后的输入矩阵。

示例

以下程序使用嵌套的for循环返回按行和列排列的已给输入矩阵的排序矩阵 –

# creating a function for sorting each row of matrix row-wise
def sortingMatrixByRow(inputMatrix, m):
   #  traversing till the length of rows of a matrix
      for p in range(m):
               # Sorting the current row
         for q in range(m-1):
            # checking whether the current element is greater than the next element
               if inputMatrix[p][q] >inputMatrix[p][q + 1]:
                  # swapping the elements using a temporary variable
                  # if the condition is true
                  tempVariable = inputMatrix[p][q]
                  inputMatrix[p][q] = inputMatrix[p][q + 1]
                  inputMatrix[p][q + 1] = tempVariable

# creating a function to get the transpose of a matrix
# by accepting the input matrix, m values as arguments
def transposeMatrix(inputMatrix, m):
   # traversing through the rows of a matrix
      for p in range(m):
         # Traversing from row +1 column to last column
         for q in range(p + 1, m):
            # Swapping the element at index (p,q) with (q,p)
            temp = inputMatrix[p][q]
            inputMatrix[p][q] = inputMatrix[q][p]
            inputMatrix[q][p] = temp

# creating a function for sorting the matrix rows column-wise
def sortMatrixRowandColumn(inputMatrix, m):
   # sorting the rows of an input matrix by
   # calling the above defined sortingMatrixByRow() function
   sortingMatrixByRow(inputMatrix, m)
   # getting the transpose of an input matrix by
   # calling the above defined transposeMatrix() function
   transposeMatrix(inputMatrix, m)
   # once again sorting the rows of an input matrix by
   # calling the above defined sortingMatrixByRow() function
   sortingMatrixByRow(inputMatrix, m)
   # once again getting the transpose of an input matrix(So we sorted the columns)
   transposeMatrix(inputMatrix, m)

# creating a function to print the matrix
def printingMatrix(inputMatrix, rows):
   # Traversing in the rows of the input matrix
   for i in range(rows):
      # Traversing in the columns corresponding to the current row
         # of the input matrix
         for j in range(rows):
            print(inputMatrix[i][j], end=" ")
         # Printing a new line to separate the rows
         print()
# input matrix
inputMatrix = [[2, 6, 5],
               [1, 9, 8],
               [7, 3, 10]]

# input m value representing 3x3 matrix
# (dimensions)
m = 3
print("Input Matrix:")
# printing the input matrix by calling the above
# printingMatrix() function
printingMatrix(inputMatrix, m)
# calling the above defined sortMatrixRowandColumn() function
# by passing the input matrix, m values to it to
# sort the matrix row and column-wise
sortMatrixRowandColumn(inputMatrix, m)
# printing the input matrix after sorting row and column-wise
# by calling the above printingMatrix() function
print("Input Matrix after sorting row and column-wise:")
printingMatrix(inputMatrix, m)

输出

执行以上程序后,将生成以下输出结果:

Input Matrix:
2 6 5 
1 9 8 
7 3 10 
Input Matrix after sorting row and column-wise:
1 5 6 
2 7 9 
3 8 10

时间复杂度 − O(n^2 log2n)

辅助空间 − O(1)

结论

在本文中,我们学习了如何使用Python按行和按列对给定的矩阵进行排序。此外,我们还学习了如何转置给定的矩阵,并如何使用嵌套的for循环(而不是使用内置的sort()方法)按行对矩阵进行排序。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程