Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java中的二维排序(不带.Sort())_Java_Sorting - Fatal编程技术网

Java中的二维排序(不带.Sort())

Java中的二维排序(不带.Sort()),java,sorting,Java,Sorting,我明天有一个期末考试,虽然我已经找到了解决办法,但我希望有人能解释一下为什么必须运行下面显示的代码两次才能将列表从1,2,3等重新排列到3,2,1。。如果我只使用sortColumn函数一次,它将返回3、1、2等 public class test1 { public static void main(String[] args) { // TODO Test skills with sorting algorithms int[][] num = { { 1, 2

我明天有一个期末考试,虽然我已经找到了解决办法,但我希望有人能解释一下为什么必须运行下面显示的代码两次才能将列表从1,2,3等重新排列到3,2,1。。如果我只使用sortColumn函数一次,它将返回3、1、2等

public class test1 {

  public static void main(String[] args) {
      // TODO Test skills with sorting algorithms
      int[][] num = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
      int[][] newNum = sortColumn(num);
      // newNum = sortColumn(newNum);
      for (int row = 0; row < newNum.length; row++) {
          System.out.println(" ");
          for (int col = 0; col < newNum[0].length; col++) {
              System.out.print(newNum[row][col]);
          }
      }

  }

  static int[][] sortColumn(int[][] num) {
      int[][] colNum = num.clone();
      // System.out.println(colNum.length);
      // System.out.println(colNum[0].length);
      int temp;
      for (int row = colNum.length - 1; row > -1; row--) {
          for (int col = colNum[0].length - 2; col > -1; col--) {
              if (colNum[row][col] < colNum[row][(col + 1)]) {
                temp = colNum[row][col];
                colNum[row][col] = colNum[row][(col + 1)];
                colNum[row][(col + 1)] = temp;
              }
          }
      }

      return colNum;
  }

}
公共类test1{
公共静态void main(字符串[]args){
//使用排序算法的TODO测试技能
int[]num={{1,2,3},{4,5,6},{7,8,9};
int[][]newNum=sortColumn(num);
//newNum=sortColumn(newNum);
for(int row=0;row-1;row--){
对于(int col=colNum[0]。长度-2;col>-1;col--){
if(colNum[row][col]
该算法从数组中倒数第二个元素开始,并将其与最后一个元素进行比较,因此将
2
3
进行比较,从而交换它们,留下1、3、2

接下来,您将继续处理数组的第一个元素,并将其与第二个元素进行比较,从而将
1
3
进行比较,这两个元素再次交换,留下3,1,2

但是,如果没有第二次调用
sortColumn
,代码将不会比较
1
2
,因此它们保持原样


第二次调用
sortColumn
的问题是,如果您添加第四列(而且添加的列越多,情况就越糟),您将回到同一条船上:从1、2、3、4开始,两次调用
sortColumn
会产生4、3、1、2。从理论上讲,调用
sortColumn
的次数可能比列数少一次,但如果得到的列数超过几列,则效率会降低,因为您正在比较已排序到正确位置的列。

谢谢,这确实有助于将其放在正确的位置。我想是时候开始使用.sort功能了。