Task is to sort each column of a matrix in an ascending and descending order interchangeably so that for instance first column is sorted in ascending order, second in descending, third in ascending and so on...
只能使用普通数组和矩阵,因此不能使用 HashMap 、集合、列表或类似的内容。
到目前为止,我已经有了这个,这只是一个想法,但我必须承认我已经卡在这里了。
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]);
}
}
最佳答案
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();
}
}
关于java - java 按升序对矩阵的列进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48084034/