对二维整数数组按列进行排序

huangapple go评论67阅读模式
英文:

Sorting 2D array of integers by column

问题

我需要在Java中构建一个方法,其中输入是一个整数的二维数组,结果是一个整数的二维数组,其中每个元素都引用列中的一个元素位置。让我用一个示例来解释。将一个5x5的二维数组作为方法的输入,如下所示:

int[][] array = new int[][]{
        {124, 188, 24, 254, 339},
        {0, 7, 77, 145, 159},
        {206, 340, 280, 523, 433},
        {310, 265, 151, 411, 398},
        {24, 104, 0, 183, 198}};

现在,我需要根据以下方式构建一个新的整数的二维数组(以下将其称为 newArray ):

  • 列 0 中的最小值是 0,与行 1 相关联(因此,我需要在 newArray[0][0] 中分配 1)。

  • 接下来,列 0 中的最小值是 24,与行 4 相关联(因此,我需要在 newArray[1][0] 中分配 4)。

  • 然后,列 0 中的最小值是 124,与行 0 相关联(因此,我需要在 newArray[2][0] 中分配 0)。

  • 对于每一列依此类推...

该方法的最终输出应该类似于以下二维数组。

英文:

I need to build a method in Java where the input is a 2D array of integers and get as a result a 2D array of integers where each element makes reference to a position of an element in a column. Let me explain that with an example. Consider as an input for the method a 2D arrays of 5x5 as follow:

int[][] array = new int[][]{
        {124, 188, 24, 254, 339},
        {0, 7, 77, 145, 159},
        {206, 340, 280, 523, 433},
        {310, 265, 151, 411, 398},
        {24, 104, 0, 183, 198}};

Now I need to build a new 2D array of integer (I will call newArray in the following) according to:

  • The minimum value of column 0 in the array is 0 and is associated with row 1 (then, I need to assign a 1 in newArray[0][0]).

  • Next, the minimum value of column 0 in the array is 24 and is associated with row 4 (then, I need to assign a 4 in newArray[1][0]).

  • Then, the minimum value of column 0 in the array is 124 and is associated with row 0 (then, I need to assign a 0 in newArray[2][0]).

  • And so on for each column...

The final output of the method must be something like the following 2d array.

对二维整数数组按列进行排序

Any help would be highly appreciated.

答案1

得分: 1

如果我理解正确:

输入:
{{124,188,24,254,339},
{0,7,77,145,159},
{206,340,280,523,433},
{310,265,151,411,398},
{24,104,0,183,198}}

输出:
{{1,1,4,1,1}
{4,4,0,4,4}
{0,0,1,0,0}
{2,3,3,3,3}
{3,2,2,2,2}}

以下是代码:

public static int[][] createArray(int[][] a) {
    int[][] nA = new int[a.length][a[0].length];
    int[] col = new int[a.length];
    int minIndex = -1;
    for (int i = 0; i < a.length; i++) {
        // 首先取出列
        for (int j = 0; j < a[0].length; j++) {
            col[j] = a[j][i];
        }
        // 遍历列
        for (int k = 0; k < a[0].length; k++) {
            int min = Integer.MAX_VALUE;
            // 遍历列中剩余的数字
            for (int j = 0; j < col.length; j++) {
                // 找到剩余数字中的最小值
                if (min > col[j]) {
                    min = col[j];
                    minIndex = j;
                }
            }
            // 从数组中删除该数字
            col[minIndex] = Integer.MAX_VALUE;
            // 在最终数组中设置这个数字
            nA[k][i] = minIndex;
        }
    }
    return nA;
}

可能有更简单的方法,但这个方法是有效的!

英文:

If I understood correctly :

IN :
     {{124, 188, 24,  254, 339},
      {0,   7,   77,  145, 159},
      {206, 340, 280, 523, 433},
      {310, 265, 151, 411, 398},
      {24,  104, 0,   183, 198}}

OUT :
     {{1, 1, 4, 1, 1}
      {4, 4, 0, 4, 4}
      {0, 0, 1, 0, 0}
      {2, 3, 3, 3, 3}
      {3, 2, 2, 2, 2}

Here's the code :

public static int[][] createArray(int[][] a) {
    int[][] nA = new int[a.length][a[0].length];
    int[] col = new int[a.length];
    int minIndex = -1;
    for (int i = 0; i &lt; a.length; i++) {
        // First get the col out
        for (int j = 0; j &lt; a[0].length; j++) {
            col[j] = a[j][i];
        }
        // Loop through the col
        for (int k = 0; k &lt; a[0].length; k++) {
            int min = Integer.MAX_VALUE;
            // Loop through the remaining numbers of the col
            for (int j = 0; j &lt; col.length; j++) {
                // Find the remaining lowest number
                if (min &gt; col[j]) {
                    min = col[j];
                    minIndex = j;
                }
            }
            // Delete the number from the array
            col[minIndex] = Integer.MAX_VALUE;
            // Set this number in the final array
            nA[k][i] = minIndex;
        }
    }
    return nA;
}

There might be an easier way, but it works !

答案2

得分: 0

按列排序矩阵元素的索引,实际上是通过转置矩阵的行来对元素的索引进行排序:

int m = 5;
int n = 6;
int[][] arr1 = new int[][]{
        {124, 188, 24, 254, 339, 3},
        {0, 7, 77, 145, 159, 1},
        {206, 340, 280, 523, 433, 5},
        {310, 265, 151, 411, 398, 4},
        {24, 104, 0, 183, 198, 2}};
int[][] arr2 = IntStream
        // 遍历数组的行索引
        .range(0, n)
        .mapToObj(i -> IntStream
                // 遍历数组的列索引
                .range(0, m)
                .boxed()
                // 根据数组中的值对列的索引进行排序
                .sorted(Comparator.comparingInt(j -> arr1[j][i]))
                .mapToInt(Integer::intValue)
                // 排序后的索引列即为新数组的一行
                .toArray())
        // 返回排序后的索引数组
        .toArray(int[][]::new);
// 转置索引数组
int[][] arr3 = new int[m][n];
IntStream.range(0, m).forEach(i ->
        IntStream.range(0, n).forEach(j ->
                arr3[i][j] = arr2[j][i]));
// 输出
Arrays.stream(arr3).map(Arrays::toString).forEach(System.out::println);

输出结果:

[1, 1, 4, 1, 1, 1]
[4, 4, 0, 4, 4, 4]
[0, 0, 1, 0, 0, 0]
[2, 3, 3, 3, 3, 3]
[3, 2, 2, 2, 2, 2]
英文:

Sorting the indexes of matrix elements by columns is sorting the indexes of elements by the rows of a transposed matrix:

int m = 5;
int n = 6;
int[][] arr1 = new int[][]{
        {124, 188, 24, 254, 339, 3},
        {0, 7, 77, 145, 159, 1},
        {206, 340, 280, 523, 433, 5},
        {310, 265, 151, 411, 398, 4},
        {24, 104, 0, 183, 198, 2}};
int[][] arr2 = IntStream
        // iterate over the indices
        // of the rows of the array
        .range(0, n)
        .mapToObj(i -&gt; IntStream
                // iterate over the
                // indices of the columns
                .range(0, m)
                .boxed()
                // sort indices of the elements of the
                // columns by its values in the array
                .sorted(Comparator.comparingInt(j -&gt; arr1[j][i]))
                .mapToInt(Integer::intValue)
                // sorted column of indices is
                // a row in the new array
                .toArray())
        // return sorted array of indices
        .toArray(int[][]::new);
// transpose the array of indices
int[][] arr3 = new int[m][n];
IntStream.range(0, m).forEach(i -&gt;
        IntStream.range(0, n).forEach(j -&gt;
                arr3[i][j] = arr2[j][i]));
// output
Arrays.stream(arr3).map(Arrays::toString).forEach(System.out::println);

Output:

[1, 1, 4, 1, 1, 1]
[4, 4, 0, 4, 4, 4]
[0, 0, 1, 0, 0, 0]
[2, 3, 3, 3, 3, 3]
[3, 2, 2, 2, 2, 2]

huangapple
  • 本文由 发表于 2020年10月21日 04:32:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/64452919.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定