使用Python對矩陣進行行排序和列排序
在本文中,我們將學習一個Python程式,用於對矩陣進行行排序和列排序。
假設我們已經輸入了一個M x M矩陣。我們將使用巢狀for迴圈對給定的輸入矩陣進行行排序和列排序。
演算法(步驟)
以下是執行所需任務的演算法/步驟:−
建立一個函式sortingMatrixByRow(),透過接受輸入矩陣m(行數)作為引數來對矩陣的每一行(即行方向)進行排序。
在函式內部,使用for迴圈遍歷矩陣的行。
使用另一個巢狀的for迴圈遍歷當前行的所有列。
使用if條件語句檢查當前元素是否大於下一個元素。
如果條件為true,則使用臨時變數交換元素。
建立另一個函式transposeMatrix(),透過接受輸入矩陣m(行數)作為引數來獲取矩陣的轉置。
使用for迴圈遍歷矩陣的行。
使用另一個巢狀for迴圈遍歷從(row +1)列到列末尾的元素。
將當前行、列元素與列、行元素交換。
建立一個函式sortMatrixRowandColumn(),透過接受輸入矩陣m(行數)作為引數來對矩陣進行行排序和列排序。
在函式內部,呼叫上面定義的sortingMatrixByRow()函式對輸入矩陣的行進行排序。
呼叫上面定義的transposeMatrix()函式獲取輸入矩陣的轉置。
再次呼叫上面定義的sortingMatrixByRow()函式對輸入矩陣的行進行排序。
再次呼叫上面定義的transposeMatrix()函式獲取輸入矩陣的轉置。
建立一個函式printingMatrix(),使用巢狀for迴圈遍歷矩陣的行和列來列印矩陣。
建立一個變數來儲存輸入矩陣。
建立另一個變數來儲存輸入m(行數)值。
呼叫上面定義的printingMatrix()函式來列印輸入矩陣。
透過將輸入矩陣和m值傳遞給它來呼叫上面定義的sortMatrixRowandColumn()函式,對矩陣進行行和列排序。
透過呼叫上面定義的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()方法)對矩陣進行行排序。
資料結構
網路
關係資料庫管理系統(RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP