【问题标题】:Sort columns of a matrix in ascending order java按升序对矩阵的列进行排序java
【发布时间】:2018-01-03 19:36:02
【问题描述】:

任务是对矩阵的每一列进行升序和降序排序 可互换顺序,例如第一列被排序 升序,降序第二,升序第三等等......

只能使用普通数组和矩阵,所以不能使用 Hashmaps、Sets、Lists 或类似的东西。

到目前为止,我有这个,这只是一个想法,但我必须承认我已经卡在这里了。

public class TwoDimArray {

static void enterMatrix(int[][] a, int m, int n) {

    Scanner scan = new Scanner(System.in);

    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            System.out.println("Enter " + i + " column matrice...\nEnter" + j + " row matrice...");

            a[i][j] = scan.nextInt();

        }

    }
    System.out.println("Final matrix\n");
    printMatrix(a, m, n);
}

static void printMatrix(int[][] a, int m, int n) {
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            System.out.print(a[i][j]);

        }
        System.out.println();

    }

}

static void sortMatriceColumn(int[] a, int n) {
// My idea was to create static method like this and call it for each column 
//while iterating through matrix, so as the method for descending sort, but 
//I am not quite sure of how to 
//implement 
// this to the end

    int temp;

    for (int i = 0; i < n; i++)

    {
        for (int j = i + 1; j < n; j++) {
            if (a[i] > a[j]) {
                temp = a[i];
                a[i] = a[j];
                a[j] = temp;
            }
        }
    }
    System.out.print("Ascending Order:");
    for (int i = 0; i < n - 1; i++) {
        System.out.print(a[i] + ",");
    }
    System.out.print(a[n - 1]);

}

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter number of matrix rows and cols...");
    int rowNum= scan.nextInt();
    int colNum= scan.nextInt();
    int[][] a = new int[rowNum][colNum];
    enterMatrix(a, rowNum, colNum);

}

}

编辑:

另外,我在这里越界了。

   static void sortMatriceColumn(int[][] a, int rowNum, int colNum) 
   {
    //int temp;
    int i,j = 0,k;

    for ( i = 0; i < rowNum; i++) {
        for ( j = 0; j < colNum; j++) {
            for ( k = j + 1 ; k < colNum; k++) {
                if (a[i][j] > a[i][k]) {
                    int temp1= a[i][j]; 
                    a[i][j]=a[i][k];
                    a[i][k]=temp1;



                }
            }
        }
    }
    for(int l11 = 0; l11 < rowNum-1 ; l11++) {
        System.out.print(" " + a[l11][j]);
    }
  }

【问题讨论】:

  • 这看起来像是一个家庭作业,你已经完成了大约 80%。您的列排序功能几乎是冒泡排序,但您错过了查看数组是否已排序的测试。接下来,要么实现升序和降序版本,要么使您的分拣机能够根据输入参数前进或后退。您可以谷歌冒泡排序 java,或者只是排序 java 以查看示例代码和排序选项。

标签: java arrays sorting matrix


【解决方案1】:
   for(int i=0; i<n;i++){
       for(j=0;j<m;j++){
           for(k=j;k<m;k++){
               if(a[i][j]>a[i][k]){
                 swap(a[i][j],a[i][k]);
               }
           }
      }

对于每一列,我实际上都做了你所做的,但是我将二维数组发送到排序函数,在函数内部我对一列进行排序并继续到下一列。

您的想法非常好,但是您将其实现为一维,(如果我们需要对行而不是列进行排序,这实际上会很好,因为行本身确实是一个数组,列是不是)。 希望它有所帮助:)

编辑:你的打印不好,试试这个:

for(int r=0;r<colNum;r++){
    for(int m = 0; m < rowNum ; m++) {
        System.out.print(" " + a[m][r]);
        }
    System.out.println();
}

另一个编辑:

static void enterMatrix(int[][] a, int m, int n) {

Scanner scan = new Scanner(System.in);

 for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            System.out.println("Enter " + i + " column matrice...\nEnter" + j + " row matrice...");

            a[i][j] = scan.nextInt();

        }

    }
    System.out.println("Final matrix\n");
    printMatrix(a, m, n);
}

static void printMatrix(int[][] a, int m, int n) {
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            System.out.print(a[i][j]);

        }
        System.out.println();

    }

}

 static void sortMatriceColumn(int[][] a, int rowNum, int colNum) 
   {
    //int temp;
    int i,j = 0,k;

    for ( i = 0; i < colNum; i++) {
        for ( j = 0; j < rowNum; j++) {
            for ( k = j + 1 ; k < rowNum; k++) {
                if(i%2==0){
                if (a[j][i] > a[k][i]) {
                    int temp1= a[j][i]; 
                    a[j][i]=a[k][i];
                    a[k][i]=temp1;
                }
                }else{
                    if (a[j][i] < a[k][i]) {
                        int temp1= a[j][i]; 
                        a[j][i]=a[k][i];
                        a[k][i]=temp1;
                    }
                }
            }
        }
    }
    for(int r=0;r<colNum;r++){
        for(int m = 0; m < rowNum ; m++) {
            System.out.print(" " + a[r][m]);
            }
        System.out.println();
    }
  }

【讨论】:

  • 我应该在交换方法中使用冒泡排序?
  • 你能帮我解决这个问题吗,我明白了,但不知何故我一直在实施它。
  • @T_dejan 嗨,我实际上并没有写它,因为你在代码中得到了它,在 sortMatricecolumn 函数中,你做了完全交换,你得到一个整数,比如说 temp(就像你在您的代码),它保存 a[i][j] 值,将 a[i][k] 值放入 a[i][j],然后将 temp 放入 a[i][j]。很抱歉,如果它搞砸了,我实际上不知道如何在评论中提交代码,但我希望它是可以理解的。如果没有,请在此处发表评论。至于第一个问题,交换只是在 2 个值之间,如果你结合所有交换,我们在选择排序中实际做了什么。
  • 当你找到一些时间可以在你的评论中编辑它时,我试着按照你说的去做,但没有成功。
  • int temp= a[i][j]; a[i][j]=a[i][k]; a[i][k]=temp;
猜你喜欢
  • 1970-01-01
  • 2012-04-22
  • 1970-01-01
  • 2017-04-24
  • 2017-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-27
相关资源
最近更新 更多