简体   繁体   中英

Sort 2D array in ascending order

        class arrayDemo {

            static void sort2D(int[][] B) {

             boolean swap = true;
             int oy=0;
             int temp=0;

             for(int ox=0;ox<B.length;ox++){
                 while(oy<B[ox].length) {
                     while(swap) {
                     swap = false;
                         for(int ix=0;ix<B.length;ix++) {
                             for(int iy=0;iy<B[ix].length;iy++) {
                                     if(B[ox][oy]<B[ix][iy]) {
                                     temp = B[ix][iy];
                                     B[ix][iy] = B[ox][oy];
                                     B[ox][oy] = temp;
                                     swap = true;
                                     }
                                 }
                             }           
                     }
                 oy++; 
                 }
             } 
             for(int row=0;row<B.length;row++)
             for(int col=0;col<B[row].length;col++)
             System.out.println(B[row][col]);
             }

public static void main(String...S) {

     int y[][] = {{10,20,0,30},{10,5,8},{3,9,8,7},{2,3}};
     sort2D(y);
}    
}

I am trying to sort a 2D array in ascending order.

Input: {{10,20,0,30},{10,5,8},{3,9,8,7},{2,3}}; Output: 30,20,10,10,9,8,8,7,5,3,0,2,3

Can someone help me know what is wrong with my code.

You are comparing elements that are not in the same row or column. Each sub-array should be sorted individually. You might want to reconsider this line if (B[ox][oy] < B[ix][iy]) .

That code has a number of problems.

  1. It throws ArrayIndexOutOfBoundsException . This is because all for loop tests test against B.length , which is not correct for inner arrays.
  2. You are comparing every pair of elements, but some pairs are the reverse of other pairs, and the reverse pairs should not be tested. You need to limit the scope of your inner set of for loops, by starting at a different index.

To fix all these problems, the path of least resistance is to dump the 2D array into a 1D array and sort that, which is much easier.

Here is code that has been tested and shown to work:

static void sort2D(int[][] B) {

        int count = 0;
        for (int[] is : B)
            for (int i : is)
                count++;
        int[] A = new int[count];
        count = 0;
        for (int[] is : B)
            for (int i : is)
                A[count++] = i;

        int temp;
        for (int i = 0; i < A.length; i++)
            for (int j = i + 1; j < A.length; j++)
                if (A[i] > A[j]) {
                    temp = A[i];
                    A[i] = A[j];
                    A[j] = temp;
                }
        for (int i = 0; i < A.length; i++)
                System.out.print(A[i] + ",");

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM