Java程序对二维数组进行左对角线排序
对于给定的二维数组,我们的任务是编写一个 Java 程序来对其左对角线上的元素进行排序。对于此问题,我们需要以这样一种方式排列数组的元素,即从左上到右下(即数组的左对角线)读取时,值是有序的。
示例场景
Input: matrix = {3, 8, 2}, {6, 5, 4}, {3, 2, 1}; Output: new_matrix = {1, 4, 2}, {2, 3, 8}, {3, 6, 5}
使用以下排序工具来理解问题
4 | 9 | 8 |
2 | 2 | 7 |
6 | 5 | 5 |
二维数组的左对角线升序排序
这里的想法是找到对角线的长度,它是从起始点开始的剩余行和列的最小值,并创建一个存储对角线元素的数组。然后,我们可以使用 Arrays.sort() 方法对该数组进行排序,以获取按升序排列的对角线元素。
示例
让我们看看实际演示 -
import java.util.Arrays; public class DgnlSortPrgrm { public static void main(String[] args) { int[][] twoDArr = { {3, 8, 2}, {6, 5, 4}, {3, 2, 1} }; System.out.println("Matrix without sorting its diagonal::"); printMatrix(twoDArr); // calling method to sort sorting(twoDArr); System.out.println("Matrix after sorting its diagonal::"); printMatrix(twoDArr); } // method to sort diagonals public static void sorting(int[][] twoDArr) { int n = twoDArr.length; int m = twoDArr[0].length; // Sort diagonal that starts from the first row for (int col = 0; col < m; col++) { sortDgnlElem(twoDArr, 0, col); } // Sort diagonal that starts from the first column for (int row = 1; row < n; row++) { sortDgnlElem(twoDArr, row, 0); } } // method to sort elements of the diagonal private static void sortDgnlElem(int[][] twoDArr, int row, int col) { int n = twoDArr.length; int m = twoDArr[0].length; int len = Math.min(n - row, m - col); int[] dgnl = new int[len]; // getting diagonal elements for (int i = 0; i < len; i++) { dgnl[i] = twoDArr[row + i][col + i]; } // sorting diagonal elements Arrays.sort(dgnl); // sorted elements of the matrix for (int i = 0; i < len; i++) { twoDArr[row + i][col + i] = dgnl[i]; } } // method to print the matrix private static void printMatrix(int[][] matrix) { for (int[] row : matrix) { for (int elem : row) { System.out.print(elem + " "); } System.out.println(); } } }
运行此代码后,您将获得以下结果 -
Matrix without sorting its diagonal:: 3 8 2 6 5 4 3 2 1 Matrix after sorting its diagonal:: 1 4 2 2 3 8 3 6 5
二维数组的左对角线降序排序
在这种方法中,我们遵循相同的逻辑,但进行了细微的更改。在这里,我们首先对对角线的元素进行排序,然后反转它们的顺序。
示例
此 Java 程序演示了如何对二维数组的左对角线进行降序排序。
import java.util.Arrays; public class DgnlSortPrgrm { public static void main(String[] args) { int[][] twoDArr = { {3, 8, 2}, {6, 5, 4}, {3, 2, 1} }; System.out.println("Matrix without sorting its diagonal::"); printMatrix(twoDArr); // calling method to sort sorting(twoDArr); System.out.println("Matrix after sorting its diagonal::"); printMatrix(twoDArr); } // method to sort diagonals public static void sorting(int[][] twoDArr) { int n = twoDArr.length; int m = twoDArr[0].length; // Sort diagonal that starts from the first row for (int col = 0; col < m; col++) { sortDgnlElem(twoDArr, 0, col); } // Sort diagonal that starts from the first column for (int row = 1; row < n; row++) { sortDgnlElem(twoDArr, row, 0); } } // method to sort elements of the diagonal private static void sortDgnlElem(int[][] twoDArr, int row, int col) { int n = twoDArr.length; int m = twoDArr[0].length; int len = Math.min(n - row, m - col); int[] dgnl = new int[len]; // getting diagonal elements for (int i = 0; i < len; i++) { dgnl[i] = twoDArr[row + i][col + i]; } // sorting diagonal elements Arrays.sort(dgnl); // reversing the sorted array for descending order for (int i = 0; i < len / 2; i++) { int temp = dgnl[i]; dgnl[i] = dgnl[len - i - 1]; dgnl[len - i - 1] = temp; } // sorted elements of the matrix for (int i = 0; i < len; i++) { twoDArr[row + i][col + i] = dgnl[i]; } } // method to print the matrix private static void printMatrix(int[][] matrix) { for (int[] row : matrix) { for (int elem : row) { System.out.print(elem + " "); } System.out.println(); } } }
执行代码后,它将显示以下输出 -
Matrix without sorting its diagonal:: 3 8 2 6 5 4 3 2 1 Matrix after sorting its diagonal:: 5 8 2 6 3 4 3 2 1
广告