不使用Arrays.sort()对2D数组排序

时间:2019-11-15 16:51:32

标签: java multidimensional-array

我正在尝试从最小到最大对整个数组进行排序,但是我无法使用Arrays.sort()之类的任何方法。到目前为止,我只能创建数组。我的输出示例为

原始数组为:

74 7 71

59 4 37

排序后的数组是:

4 7 37

59 71 74


import java.util.Random;

public class arrays2d2 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Random rand = new Random();

        int[][] array2D = new int[3][3]; 

        System.out.println("");
        System.out.println("The original array is: " );

        for (int i = 0; i < array2D.length; i++) { 
            for (int j = 0; j < array2D[i].length; j++) { 
                array2D[i][j] = rand.nextInt(100);
                System.out.print(array2D[i][j] + " "); 
            } 
            System.out.println(); 
        } 
        System.out.println("");

        System.out.println("The sorted array is: ");
        for (int i = 0; i < array2D.length; i++) { 
            for (int j = 0; j < array2D[i].length; j++) 
                System.out.print(array2D[i][j] + " "); 
            System.out.println(); 
        } 

    }

}

3 个答案:

答案 0 :(得分:0)

有很多方法可以做到这一点。

您可以使用Arrays.sort将二维数组复制为一维数组:

int[] copy = new int[array2d.length * array2d[0].length]; //creates the copy array
for(int i = 0; i < array2d.length; i++)
    for(int j = 0; j < array2d[0].length; j++)
       copy[i*array2d.length + j] = array2d[i][j];

Arrays.sort(copy);

for(int i = 0; i < array2d.length; i++)
    for(int j = 0; j < array2d[0].length; j++)
       array2d[i][j] = copy[i*array2d.length + j];

请注意,此算法仅适用于矩形数组(如您的矩形数组)

答案 1 :(得分:0)

您可以执行以下操作:

public class Arrays2D2 {

    public static void main(String[] args) {

        int[][] array2D = { { 74, 7, 71 }, { 59, 4, 37 } };

        int[] n = new int[6];
        int counter = 0;

        //Copy elements from the 2-D array to a 1-D array
        for (int i = 0; i < array2D.length; i++)
            for (int j = 0; j < array2D[i].length; j++)
                n[counter++] = array2D[i][j];

        //Sort the 1-D array
        int temp;
        for (int i = 1; i < n.length; i++) {
            for (int j = i; j > 0; j--) {
                if (n[j] < n[j - 1]) {
                    temp = n[j];
                    n[j] = n[j - 1];
                    n[j - 1] = temp;
                }
            }
        }

        //Copy elements from sorted 1-D array to the 2-D array
        counter=0;
        for (int i = 0; i < array2D.length; i++)
            for (int j = 0; j < array2D[i].length; j++)
                array2D[i][j]=n[counter++]  ;

        //Display the 2-D array
        for (int i = 0; i < array2D.length; i++) { 
            for (int j = 0; j < array2D[i].length; j++) {               
                System.out.print(array2D[i][j] + " "); 
            } 
            System.out.println(); 
        } 
    }
}

输出:

4 7 37 
59 71 74 

答案 2 :(得分:0)

您可以使用 for 循环对数组进行排序:

int matriks1[][] = {
        {12,21,9},
        {17,6,4},
        {25,56,78}
    };
    int temp;
    for(int i=0;i<matriks1.length;i++){
        for(int j=0; j<matriks1.length;j++){
            for(int k =0; k<matriks1.length;k++){
                for(int l = 0; l<matriks1.length; l++){
                    if(matriks1[i][j] < matriks1[k][l]){
                    temp = matriks1[k][l];
                    matriks1[k][l] = matriks1[i][j];
                    matriks1[i][j]=temp;
                    }
                }   
            }
        }
    }

输出:

4 6 9
12 17 21
25 56 78