如何对二维数组对角线元素进行排序

时间:2019-03-23 03:46:07

标签: java data-structures

   public class DiagonalSort {
   static int sortDiagonal(int m[][]) {

     int[] intArray = new int[4];
     int k=0;

//to insert 2d array diagonal elements into 1d array
     for(int i = 0; i<m.length; i++){
         for(int j= 0; j<m[i].length; j++) {
             if(i == j) {                
                 int n = m[i][j];

                 intArray[k]=n;
                 k++; 
             }
                    }
     }

     // sorting 1d array elements
     Arrays.sort(intArray);

     // inserting sorted elements to its appropriate positions
     for(int i = 0; i<m.length; i++){
         for(int j= 0; j<m[i].length; j++) {
             if(i == j) {
                m[i][j]= intArray[i];
             } 
         }
     }


     //printing the diagonal elements
     for (int i = 0; i < m.length; i++) { 
            for (int j = 0; j < m[i].length; j++) 
                System.out.print(m[i][j] + " "); 
            System.out.println(); 
        } 
    return 0;

 }

 public static void main(String args[]) 
    { 
        int m[][] = { { 36, 10, 24, 8 }, 
                      { 12, 23, 0,  2 }, 
                      { 9,  5,  10, 2 }, 
                      { 6,  3,  1,  2 } }; 
        sortDiagonal(m); 
    } }

我使用了非常简单的逻辑:

  • 首先将对角线元素插入一维数组
  • 排序一维数组
  • 将排序后的1d数组的元素插入2d数组对角线位置

始终欢迎您提供更有效的答案。

2 个答案:

答案 0 :(得分:0)

1.Your logic is good enough but you can optimize your code by removing some extra loops.
2.If you want to access only diagonal elements then you don't need to run 2 loops.
3.You can do like this

package test.code;
import java.util.Arrays;

public class DiagonalSort {
   static int sortDiagonal(int m[][]) {

     int[] intArray = new int[4];

//to insert 2d array diagonal elements into 1d array
     for(int i = 0; i<m.length; i++){
         intArray[i] = m[i][i];
     }

     // sorting 1d array elements
     Arrays.sort(intArray);

     // inserting sorted elements to its appropriate positions
     for(int i = 0; i<m.length; i++){
         m[i][i]= intArray[i];
     }


     //printing the diagonal elements
     for (int i = 0; i < m.length; i++) { 
            for (int j = 0; j < m[i].length; j++) 
                System.out.print(m[i][j] + " "); 
            System.out.println(); 
        } 
    return 0;

 }

 public static void main(String args[]) 
    { 
        int m[][] = { { 36, 10, 24, 8 }, 
                      { 12, 23, 0,  2 }, 
                      { 9,  5,  10, 2 }, 
                      { 6,  3,  1,  2 } }; 
        sortDiagonal(m); 
    } }

答案 1 :(得分:0)

**Java class to sort diagonal element**    
    

class Solution {
            public int[][] diagonalSort(int[][] mat) {
          int row=mat.length;
        int col=mat[0].length;
        for(int i=0;i<row;i++){
    
        for(int j=0;j<col;j++){
    
        for(int r=i+1,c=j+1;r<row && c<col;r++,c++){
    
        if(mat[i][j]>mat[r][c]){
    // Swap the value
        int temp=mat[i][j];
        mat[i][j]=mat[r][c];
        mat[r][c]=temp;
                }
             }
           }
        }
        return mat;
            }
        }
相关问题