2015-04-27 114 views
8

我想對多維數組進行列排序。我的代碼設置,但它不是...列對數組進行排序?

示例排序前顯示正確的結果:

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

請告訴我我做錯了什麼!

謝謝!

回答

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; 

} 
0

你的排序算法是錯誤的原因各不相同:當你發現一個新的,你的每個元素只有當它應該是比較所有元素的最低值進行比較,你不更新min ...

我可以建議2個解決方案:

  1. 看一下現有的算法,以陣列
  2. 排序轉置矩陣,上線使用Arrays.sort(),轉置 矩陣回
1

我打了一個問題的例子,希望它有幫助。

代碼

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(""); 
     } 
    } 
} 

控制檯輸出

---Before sort--- 
6.4 4.0 2.0 
4.0 2.0 4.0 
1.0 3.0 1.0 
---After sort--- 
1.0 2.0 1.0 
4.0 3.0 2.0 
6.4 4.0 4.0