如何在 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 程式語言查詢矩陣中已排序的行數的不同方法。

更新於: 2023 年 3 月 6 日

345 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告
© . All rights reserved.