如何在 Java 中查找矩阵中已排序的行数?
矩阵不过是一组以二维矩形布局排列的数据元素的集合。在 Java 中,二维数组可以被视为矩阵。
根据问题陈述,任务是计算矩阵中所有以严格递增顺序或严格递减顺序排序的行。
让我们深入研究本文,了解如何使用 Java 编程语言来实现它。
举几个例子
示例 1
Suppose the original matrix is{
{ 5, 6, 7, 8 ,
{ 5, 3, 1, 0 },
{ 10, 7, 4, 3 },
{ 13, 7, 8, 21 },
{ 5, 4, 11 ,22 },
{ 11, 2, 3 ,4 }
};
在计算矩阵中已排序的行后,结果索引将是
矩阵中已排序的行:3
示例 2
Suppose the original matrix is{
{ 3, 5, 7, 9 },
{ 5, 3, 1, 8 },
{ 10, 7, 4, 3 },
{ 4, 7, 8, 11 },
{ 0, 4, 11 ,22 },
{ 1, 2, 3 ,0 }
};
在计算矩阵中已排序的行后,结果索引将是
矩阵中已排序的行:4
算法
步骤 1 - 初始化并声明矩阵
步骤 2 - 使用 for 循环检查行的递增或递减顺序。
步骤 3 - 统计行总数。
步骤 4 - 打印结果。
多种方法
我们提供了不同方法的解决方案
使用矩阵的静态初始化
使用用户定义的方法
让我们逐一查看程序及其输出。
方法 1:使用矩阵的静态初始化
在这种方法中,矩阵元素将在程序中初始化。然后根据算法,查找矩阵中已排序的行数。
示例
public class Main {
public static void main(String arg[]){
int m = 6, n = 4; //Initialising and declaring the matrix
int mat[][] = {
{ 5, 6, 7, 8 },
{ 5, 3, 1, 0 },
{ 10, 7, 4, 3 },
{ 13, 7, 8, 21 },
{ 5, 4, 11 ,22 },
{ 11, 2, 3 ,4 }
};
int result = 0;
// counting from left to right side to count increasing order of rows
for (int i = 0; i < m; i++) {
//To check if there is any pair of element that are not in increasing order.
int j;
for (j = 0; j < n - 1; j++)
if (mat[i][j + 1] <= mat[i][j])
break;
//If the loop didn't break then all elements of current row were in increasing order
if (j == n - 1)
//count of increasing order
result++;
}
// counting from right to left side to count decreasing order of rows
for (int i = 0; i < m; i++) {
//To check if there is any pair of elements that are not in decreasing order.
int j;
for (j = n - 1; j > 0; j--)
if (mat[i][j - 1] <= mat[i][j])
break;
if (n > 1 && j == 0)
result++;
}
System.out.println("The sorted rows in a matrix: " + result);
}
}
输出
The sorted rows in a matrix: 3
方法 2:使用用户定义的方法
在这种方法中,矩阵元素将在程序中初始化。然后通过将矩阵作为参数传递来调用用户定义的方法,并在方法内部根据算法查找矩阵中已排序的行数。
示例
public class Main {
public static void main(String arg[]){
//Initialising and declaring the matrix
int m = 6, n = 4;
int mat[][] = {
{ 3, 5, 7, 9 },
{ 5, 3, 1, 8 },
{ 10, 7, 4, 3 },
{ 4, 7, 8, 11 },
{ 0, 4, 11 ,22 },
{ 1, 2, 3 ,0 }
};
//calling user defined method
sort(mat, m, n);
}
// user defined method
static void sort(int mat[][], int r, int c){
//Initializing the result
int result = 0;
// counting from left to right side to count increasing order of rows
for (int i = 0; i < r; i++) {
//To check if there are any pairs of elements that are not in increasing order.
int j;
for (j = 0; j < c - 1; j++)
if (mat[i][j + 1] <= mat[i][j])
break;
//If the loop didn't break then all elements of current row were in increasing order
if (j == c - 1)
//count of increasing order
result++;
}
// counting from right to left side to count decreasing order of rows
for (int i = 0; i < r; i++) {
//To check if there is any pair of elements that are not in decreasing order.
int j;
for (j = c - 1; j > 0; j--)
if (mat[i][j - 1] <= mat[i][j])
break;
//If the loop didn't break then all elements of current row were in decreasing order
if (c > 1 && j == 0)
//count of decreasing order
result++;
}
//print the result
System.out.println("The sorted rows in a matrix: " + result);
}
}
输出
The sorted rows in a matrix: 4
在本文中,我们探索了使用 Java 编程语言查找矩阵中已排序的行数的不同方法。
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP