按升序java对矩阵的列进行排序

时间:2018-01-03 19:36:02

标签: java arrays sorting matrix

  

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

只能使用普通数组和矩阵,因此没有Hashmaps,集合,列表或类似内容。

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

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]);
    }
  }

1 个答案:

答案 0 :(得分:2)

   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]);
               }
           }
      }

对于每一列,我实际上做了你所做的,但是我将2维数组发送到了排序函数,在函数内部我对一列进行了排序,然后进入下一列。

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

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

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