Java 查找矩阵中排序的行数

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编程语言探讨了寻找矩阵中排序行数的不同方法。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程