Java 如何按递增顺序对二维数组进行排序?爪哇

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20206476/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 00:17:10  来源:igfitidea点击:

How to sort a 2D array in increasing order? Java

javaarrays2d

提问by user3033712

I have a test tomorrow where we write a code based on what is asked. I need some explanation on how to sort a 2D array in increasing order. I can do this for a 1D array but I'm not sure if the same code will work for the 2D. Can you just explain how to implement this for a 2D array in your own way, I don't want you to think this is for homework, I just need to know how to do this for a test tomorrow. Thanks

我明天有一个测试,我们根据要求编写代码。我需要一些关于如何按递增顺序对二维数组进行排序的解释。我可以对一维数组执行此操作,但我不确定相同的代码是否适用于二维数组。你能不能用你自己的方式解释如何为一个二维数组实现这个,我不想让你认为这是家庭作业,我只需要知道如何为明天的测试做这个。谢谢

for (i = 0; i < a.length - 1; i++) {
 for (j = i+1; j < a[0].length; j++) {
      if (a[i] < a[j]) {
           int temp = a[i];
           a[i] = a[j];
           a[j] = temp;
           System.out.print(temp);
      }
 }

}

}

采纳答案by Julien

It seems that you want to sort each line of your matrix. You could simply go over each line and sort using off-the-shelf java method, given ais a two dimensional array :

似乎您想对矩阵的每一行进行排序。您可以简单地检查每一行并使用现成的 java 方法进行排序,给出的a是一个二维数组:

for (i = 0; i < a.length; i++) {
  Arrays.sort(a[i]);
}

Anyway your question is not crystal clear to me and I join @Normr on its comment.

无论如何,你的问题对我来说并不是很清楚,我加入了@Normr 的评论。

回答by 3yakuya

One way to to it is basically to implement a selection sort (so you iterate through elements in order in which you want elements to be sorted, and for each element you search through the rest of your table - EXCLUDING the elements before your current element - select the smallest element and swap it with your current element). This would be O(n^2) where n is the total size of your array.

一种方法基本上是实现选择排序(因此您可以按希望元素排序的顺序遍历元素,并为每个元素搜索表格的其余部分 - 排除当前元素之前的元素 -选择最小的元素并将其与当前元素交换)。这将是 O(n^2) ,其中 n 是数组的总大小。

Another way is to copy your elements to a 1d array, use any correct algorithm to sort it and then copy your ordered elements to correct places in your 2d array (so that it is sorted in the way you want it). With a proper data set this could be O(n) as it takes O(n) for copying and could take O(n) for Counting Sort or Bucket Sort (if they would be appropriate for your set). In the worst case this would be O(nlogn), as you could use MergeSort, HeapSort, QuickSort or any comparison-based sorting algorithm.

另一种方法是将您的元素复制到一维数组,使用任何正确的算法对其进行排序,然后将有序元素复制到二维数组中的正确位置(以便按您想要的方式对其进行排序)。使用适当的数据集,这可能是 O(n),因为它需要 O(n) 进行复制,并且可能需要 O(n) 进行计数排序或桶排序(如果它们适合您的集合)。在最坏的情况下,这将是 O(nlogn),因为您可以使用 MergeSort、HeapSort、QuickSort 或任何基于比较的排序算法。

回答by ice

according to your description,i think you could resolve the problem like this.

根据你的描述,我认为你可以解决这样的问题。

first,loop the 2D array,and put every element into a arrayList. then, use the exists method Collections.sort(List paramList) for sorting the arrayList.

首先,循环二维数组,并将每个元素放入一个arrayList。然后,使用存在方法 Collections.sort(List paramList) 对 arrayList 进行排序。

you will get a sorted list in the end.

最后你会得到一个排序的列表。

回答by Grady Wetherbee

Make the 2D array into a separate simple (1D) array (STEP 1).
Then use the Arrays.sort() method to sort the simple array (STEP 2).
Then set each space of the 2D array to be the number of columns across (X-coordinate where the space will be changed) multiplied by the number of spaces per row in the 2D array. Then add the row number (the Y-coordinate where the space will be changed) and you will have the index of the simple string that you need (STEP 3).

将二维数组变成一个单独的简单 (1D) 数组(步骤 1)。
然后使用 Arrays.sort() 方法对简单数组进行排序(步骤 2)。
然后将 2D 数组的每个空间设置为跨列数(将更改空间的 X 坐标)乘以 2D 数组中每行的空间数。然后添加行号(将更改空间的 Y 坐标),您将获得所需的简单字符串的索引(步骤 3)。

My print method is at bottom.

我的打印方法在底部。

public static void sort2DArray(int[][] arrayName) 
{
    int[] simpleArray = new int[(arrayName[0].length)*(arrayName.length)];
    for(int r = 0; r < arrayName.length; r++) //CYCLE THROUGH ROWS (Y VALUES)
    {
        for(int c = 0; c < arrayName[0].length; c++) //CYCLE THROUGH COLUMNS (X VALUES)
        {

            simpleArray[arrayName[0].length*r+c] = arrayName[r][c]; //*STEP 1*
        }
    }
    Arrays.sort(simpleArray); //*STEP 2*

    for(int r = 0; r < arrayName.length; r++) //CYCLE THROUGH ROWS (Y VALUES)
    {
        for(int c = 0; c < arrayName[0].length; c++) //CYCLE THROUGH COLUMNS (X VALUES)
        {
            arrayName[r][c] = ( simpleArray[(r * arrayName[0].length) + c ] ); //*STEP 3*
        }
    }
}

public static void print2DArrayAsTable(int[][] arrayName)  //METHOD TO PRINT A 2D ARRAY AS A TABLE
{
    for(int c = 0; c < arrayName.length; c++) //CYCLE THROUGH COLUMNS (X VALUES)
    {
        for(int r = 0; r < arrayName[0].length; r++) //CYCLE THROUGH ROWS (Y VALUES)
        {
            p.o(arrayName[c][r] + " "); //PRINT INDIVIDUAL ARRAY SPACE VALUE
        }
        p.l();
    }
}