开发者

Sorting 2d-array by last row with Arrays.sort

Is it possible to sort 2d-array by last row with Arrays.sort(,) in Java. The following snippet works great for sorting by last column but it doesn't seem to have a way to be adjusted for sortin开发者_运维问答g by last row.

My first thought was to use tranforming columns to rows, doing sort and then transforming rows to column. Any better way for very big arrays?

int[][] twoDim = { {1, 2, 3}, {3, 7, 11}, {8, 9, 16}, {4, 2,8}, {5, 3, 9} };
Arrays.sort(twoDim, new Comparator<int[]>() {
     @Override
     public int compare(int[] o1, int[] o2) {
         return ((Integer) o1[2]).compareTo(o2[2]);
     }
});

Let's elaborate the whole situation: This is where I am when my array gets initialized and by rows and columns you can imagine this dataset as following:

{1, 2, 3}, //first row with three columns

{3, 7, 11}, //second row with three columns

{8, 9, 16},

{4, 2, 8},

{5, 3, 9} //last row with three columns

Sorting by last row means to rearrange the position of first and second column because 5 is bigger than 3. So after rearranging dataset it looks like:

2, 1, 3

7, 3, 11

9, 8, 16

2, 4, 8

3, 5, 9 //now it's ordered by last row (first and second column have changed they position, by chance third column is in a right place already)


This cannot be answered if I understand what you mean by columns and rows correctly.

If you look at the dataset like this:

1, 2, 3
3, 7, 11
8, 9, 16
4, 2, 8
5, 3, 9

Now, if you sort these by the last row, you get these results:

{2, 7, 9, 2, 3}, {1,3,8,4,5}, {3, 11, 16, 8, 9}

This obviously will not be the case if you replace the 4, 2, 8 row with the 5,3,9 row. So, you have to either come up with a standard ordering, or find a different way to solve the your actual problem that you're facing.

If you are dealing with matrices, I would highly recommend a library.


Remember that two dimensional arrays are arrays of arrays. Every sort algorithm needs a facility to move the entries you want to sort. Your solution to sort by last column works, because Arrays.sort sees your inner arrays as the objects to sort. What you call sorting by last row, has no equivalent object, which should represent columns.

So you have two options:

  1. Implement your own sorting algorithm which swaps whole columns at once, but please use a text book algorithm.

  2. Transpose your matrix. But remember that this may be free, if it is possible to swap the meaning of the first and second index in the whole program.


Interesting question.

I would do it by implementing a variation of quick sort. The variations are essentially in the partition function:

  • you use the last row of your matrix as the array to be sorted
  • when swapping two elements, you actually swap two columns of your matrix

Here's an implementation:

public void qsortOnLastRow(int[][] matrix, int left, int right) {
    if (left < right) {
        int i = partition(matrix, left, right);
        qsortOnLastRow(matrix, left, i - 1);
        qsortOnLastRow(matrix, i + 1, right);
    }
}

public int partition(int[][] matrix, int left, int right) {
    int lastrow = matrix.length - 1;
    int pivotValue = matrix[lastrow][left];
    int i = left;
    for (int j = left + 1; j <= right; j++) {
        if (matrix[lastrow][j] <= pivotValue) {
            i++;
            swapColumns(matrix, i, j);
        }
    }
    swapColumns(matrix, left, i);
    return i;
}

public void swapColumns(int[][] matrix, int c0, int c1) {
    if (c0 != c1) {
        for (int i = 0; i < matrix.length; i++) {
            int t = matrix[i][c0];
            matrix[i][c0] = matrix[i][c1];
            matrix[i][c1] = t;
        }
    }
}

You can sort your int[][] matrix by calling qsortOnLastRow(matrix, 0, matrix[0].length - 1);

Complexity, if I'm not wrong, should be O(m * n * log n), where m = number of rows and n = number of columns in your matrix.

Note: You can use the same trick (sorting on the last row and swapping columns) also with other sorting algorithms.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜