给定矩阵mat[][] ,任务是按升序对矩阵的主对角元素进行排序。
主对角线:矩阵的主对角线或主对角线是元素 mat i, j的集合,其中i == j 。
例子:
Input: mat[][] = {{4, 2}, {3, 1}}
Output:
1 2
3 4
Explanation:
In the given matrix, the diagonal elements are -
=> {mat[0][0], mat[1][1]}
=> {4, 1}
=> Sorted Order = {1, 4}
Input: mat[][] = {{9, 4}, {3, 4}}
Output:
4 4
3 9
Explanation:
In the given matrix, the diagonal elements are -
=> {mat[0][0], mat[1][1]}
=> {9, 4}
=> Sorted Order = {4, 9}
方法:思路是修改选择排序,对矩阵的对角元素进行排序。矩阵 M*N 的对角线元素的计数将为min(M, N) 。我们知道矩阵的主要对角元素是 mat i, j其中 i == j。因此,矩阵主对角线的第i个元素将是 mat[i][i]。因此,重复从矩阵的主对角线上找到最小元素并将其放在开头。
下面是上述方法的实现:
C++
// C++ implementation to sort the
// major diagonal of the matrix
#include
using namespace std;
// Function to sort the major
// diagonal of the matrix
void sortDiagonal(int a[2][2], int M, int N)
{
// Loop to find the ith minimum
// element from the major diagonal
for (int i = 0; i < M; i++) {
int sm = a[i][i];
int pos = i;
// Loop to find the minimum
// element from the unsorted matrix
for (int j = i + 1; j < N; j++) {
if (sm > a[j][j]) {
sm = a[j][j];
pos = j;
}
}
// Swap to put the minimum
// element at the beginning of
// the major diagonal of matrix
swap(a[i][i], a[pos][pos]);
}
// Loop to print the matrix
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++)
cout << a[i][j] << " ";
cout << endl;
}
}
// Driven Code
int main()
{
int a[2][2] = { { 4, 2 },
{ 3, 1 } };
// Sort the major Diagonal
sortDiagonal(a, 2, 2);
return 0;
}
Java
// Java implementation to sort the
// major diagonal of the matrix
class GFG{
// Function to sort the major
// diagonal of the matrix
static void sortDiagonal(int a[][], int M, int N)
{
// Loop to find the ith minimum
// element from the major diagonal
for (int i = 0; i < M; i++) {
int sm = a[i][i];
int pos = i;
// Loop to find the minimum
// element from the unsorted matrix
for (int j = i + 1; j < N; j++) {
if (sm > a[j][j]) {
sm = a[j][j];
pos = j;
}
}
// Swap to put the minimum
// element at the beginning of
// the major diagonal of matrix
swap(a, i, i, pos, pos);
}
// Loop to print the matrix
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++)
System.out.print(a[i][j]+ " ");
System.out.println();
}
}
static void swap(int[][] a, int i, int i2, int pos, int pos2) {
int temp = a[i][i2];
a[i][i2] = a[pos][pos2];
a[pos][pos2] = temp;
}
// Driven Code
public static void main(String[] args)
{
int a[][] = { { 4, 2 },
{ 3, 1 } };
// Sort the major Diagonal
sortDiagonal(a, 2, 2);
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 implementation to sort the
# major diagonal of the matrix
# Function to sort the major
# diagonal of the matrix
def sortDiagonal(a, M, N):
# Loop to find the ith minimum
# element from the major diagonal
for i in range(M):
sm = a[i][i]
pos = i
# Loop to find the minimum
# element from the unsorted matrix
for j in range(i + 1 , N):
if (sm > a[j][j]):
sm = a[j][j]
pos = j
# Swap to put the minimum
# element at the beginning of
# the major diagonal of matrix
a[i][i], a[pos][pos] = a[pos][pos] , a[i][i]
# Loop to print the matrix
for i in range(M):
for j in range(N):
print(a[i][j],end=" ")
print()
# Driven Code
a = [[4, 2],[3, 1]]
# Sort the major Diagonal
sortDiagonal(a, 2, 2)
# This code is contributed by shubhamsingh10
C#
// C# implementation to sort the
// major diagonal of the matrix
using System;
class GFG{
// Function to sort the major
// diagonal of the matrix
static void sortDiagonal(int[,]a, int M, int N)
{
// Loop to find the ith minimum
// element from the major diagonal
for (int i = 0; i < M; i++) {
int sm = a[i, i];
int pos = i;
// Loop to find the minimum
// element from the unsorted matrix
for (int j = i + 1; j < N; j++) {
if (sm > a[j, j]) {
sm = a[j, j];
pos = j;
}
}
// Swap to put the minimum
// element at the beginning of
// the major diagonal of matrix
swap(a, i, i, pos, pos);
}
// Loop to print the matrix
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++)
Console.Write(a[i, j]+ " ");
Console.WriteLine();
}
}
static void swap(int[,] a, int i, int i2, int pos, int pos2) {
int temp = a[i, i2];
a[i, i2] = a[pos, pos2];
a[pos, pos2] = temp;
}
// Driven Code
public static void Main(String[] args)
{
int[,]a = { { 4, 2 },
{ 3, 1 } };
// Sort the major Diagonal
sortDiagonal(a, 2, 2);
}
}
// This code is contributed by 29AjayKumar
Javascript
输出:
1 2
3 4
性能分析:
- 时间复杂度: O(min(M, N) 2 )
- 辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live