列排序数组?

时间:2015-04-27 07:50:03

标签: java arrays sorting multidimensional-array

我想对多维数组进行列排序。我有代码设置,但它没有显示正确的结果......

排序前的示例:

6.0 4.0 2.0

4.0 2.0 4.0

1.0 3.0 1.0

排序后的示例:

1.0 2.0 1.0

4.0 3.0 2.0

6.0 4.0 4.0

这是我的代码:

  import java.util.Scanner;

    public class ColumnSorting
    {
   public static void main(String [] args)
   {
     run();
   }
   public static void run()
   {
      Scanner input = new Scanner(System.in);
      System.out.print("Please enter the values of your 3x3 matrix: ");
      double[][] matrix = new double[3][3];
      for (int i = 0; i < matrix.length; i++)
      {
         for (int k = 0; k < matrix[i].length; k++)
         {
            matrix[i][k] = input.nextDouble();
         }
      }
      printArrays(matrix);

    }
   public static void printArrays(double[][] matrix)
   {
   System.out.println("Before sorting: ");

       for (int i = 0; i < matrix.length; i++)
      {
        for (int k = 0; k <matrix[i].length; k++)
         {
            System.out.print(matrix[i][k] + " ");
         }
         System.out.println();
      }
    double[][] newMatrix = sortColumns(matrix);
     System.out.println();
    System.out.println("After sorting: ");

    for (int i = 0; i < newMatrix.length; i++)
      {
         for (int j =0; j < newMatrix[i].length; j++)
         {
            System.out.print(newMatrix[i][j] + " ");
         }
         System.out.println();
      }  

   }
   public static double[][] sortColumns(double[][] m)
   {
      double min;
      double temp;
      for (int i = 0; i < 3; i++)
      {
         min = m[0][i];
         for (int k = 0; k < m.length; k++)
         {

            if (min > m[k][i])
            {
               temp = min;
               m[0][i] = m[k][i];
               m[k][i] = temp;
            }
         }
      }
      return m;

   }
}

这就是我得到的:

1.0 3.0 1.0

6.0 4.0 4.0

6.0 4.0 2.0

请告诉我我做错了什么!

谢谢!

3 个答案:

答案 0 :(得分:2)

你的排序算法没有排序 - 它正在做一些奇怪的事情。

这是一个泡泡排序等同于演示。

public static double[][] sortColumns(double[][] m) {
    for (int col = 0; col < m[0].length; col++) {
        // Have we swapped one on this pass?
        boolean swapped;
        do {
            swapped = false;
            for (int row = 0; row < m.length - 1; row++) {
                // Should these be swapped?
                if (m[row][col] > m[row + 1][col]) {
                    // Swap this one with the next.
                    double temp = m[row][col];
                    m[row][col] = m[row + 1][col];
                    m[row + 1][col] = temp;
                    // We swapped! Remember to run through again.
                    swapped = true;
                }
            }
        } while (swapped);
    }
    return m;

}

虽然更好的选择是将每个列复制到一个单独的数组中,对其进行排序并将其重新放入 - 至少这样您就不需要实现自己的排序算法。

public static double[][] sortColumnsProperly(double[][] m) {
    // Sort each colum.
    for (int col = 0; col < m[0].length; col++) {
        // Pull the column out.
        double[] thisCol = new double[m.length];
        for (int i = 0; i < m.length; i++) {
            thisCol[i] = m[i][col];
        }
        // Sort it.
        Arrays.sort(thisCol);
        // Push it back in.
        for (int i = 0; i < m.length; i++) {
            m[i][col] = thisCol[i];
        }
    }
    return m;

}

答案 1 :(得分:1)

我打破了你的问题的一个例子,希望它有所帮助。

<强>代码

super.init().

控制台输出

public class Demo {

    public static void main(String[] args) {

        double[][] m = new double[4][4];

        m[0][0] = 6.4;
        m[0][1] = 4.0;
        m[0][2] = 2.0;

        m[1][0] = 4.0;
        m[1][1] = 2.0;
        m[1][2] = 4.0;

        m[2][0] = 1.0;
        m[2][1] = 3.0;
        m[2][2] = 1.0;

        System.out.println("---Before sort---");
        printSort(m);

        double[][] x = matrixColumnSort(m);

        System.out.println("---After sort---");
        printSort(x);
    }

    public static double[][] matrixColumnSort(double[][] m) {

        ArrayList<Double> arrayList = new ArrayList<Double>();
        for (int i = 0; i < m.length - 1; i++) {
            for (int j = 0; j < m.length - 1; j++) {
                arrayList.add(m[j][i]);
            }
            Collections.sort(arrayList);
            for (int j = 0; j < m.length - 1; j++) {
                m[j][i] = arrayList.get(j);
            }
            arrayList.clear();
        }
        return m;
    }

    public static void printSort(double[][] m) {
        for (int i = 0; i < m.length - 1; i++) {
            for (int j = 0; j < m.length - 1; j++) {
                System.out.print(" " + m[i][j] + " ");
            }
            System.out.println("");
        }
    }
}

答案 2 :(得分:0)

您的排序算法出于不同的原因是错误的:当您找到新的时,您不会更新private List<Bird> birdList; public Cage() { this.birdList = new ArrayList<Pigeon>(); /* Instead of birdList = new ArrayList<Bird>(); */ } ,而是在将每个元素与所有元素进行比较时将其仅与最小值进行比较...

我可以建议2个解决方案:

  1. 查看现有算法以排序数组
  2. 转置矩阵,在行上使用min,转置 矩阵回来