前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用 Python 按行和按列对矩阵进行排序

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

作者头像
很酷的站长
发布2023-02-22 13:55:57
6K0
发布2023-02-22 13:55:57
举报
文章被收录于专栏:站长的编程笔记
使用 Python 按行和按列对矩阵进行排序
使用 Python 按行和按列对矩阵进行排序

在本文中,我们将学习一个 python 程序来按行和按列对矩阵进行排序。

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

算法(步骤)

以下是执行所需任务要遵循的算法/步骤。−

  • 创建一个函数sortingMatrixByRow()来对矩阵的每一行进行排序,即通过接受输入矩阵m(行数)作为参数来逐行排序。
  • 在函数内部,使用 for 循环遍历矩阵的行。
  • 使用另一个嵌套的 for 循环遍历当前行的所有列。
  • 使用 if 条件语句检查当前元素是否大于下一个元素。
  • 如果条件为 true,则使用临时变量交换元素。
  • 创建另一个函数 transposeMatrix() 通过接受输入矩阵 m(行数)作为参数来获取矩阵的转置。
  • 使用 for 循环遍历矩阵的行。
  • 使用另一个嵌套的 for 循环遍历窗体(行 +1)列到列的末尾。
  • 将当前行、列元素与列、行元素交换。
  • 创建一个函数 sortMatrixRowandColumn() 通过接受输入矩阵 m(行数)作为参数来对矩阵行和列进行排序。
  • 在函数内部,调用上面定义的 sortingMatrixByRow() 函数对输入矩阵的行进行排序。
  • 调用上面定义的转置矩阵() 函数来获取输入矩阵的转置。
  • 再次通过调用上面定义的 sortingMatrixByRow() 函数对输入矩阵的行进行排序。
  • 再次通过调用上面定义的转置矩阵() 函数来获取输入矩阵的转置。
  • 创建一个函数 printingMatrix() 通过使用嵌套的 for 循环遍历矩阵的行和列来打印矩阵。
  • 创建一个变量来存储输入矩阵。
  • 创建另一个变量来存储输入 m(行数)值
  • 调用上面定义的 printingMatrix() 函数来打印输入矩阵。
  • 调用上面定义的sortMatrixRowandColumn()函数,方法是将输入矩阵,m值传递给它,对矩阵行和列进行排序。
  • 通过调用上面定义的 printingMatrix() 函数按行和按列排序后打印生成的输入矩阵。

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

代码语言:javascript
复制
# 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)

输出

在执行时,上述程序将生成以下输出 -

代码语言:javascript
复制
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() 方法)按行对矩阵进行排序。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 算法(步骤)
  • 输出
  • 结论
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档