[英]Sort columns of a matrix in ascending order java
任务是按升序和降序可互换地对矩阵的每一列进行排序,例如第一列按升序排序,第二列按降序排序,第三列按升序排序,依此类推...
只能使用纯数组和矩阵,因此不能使用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]);
}
}
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();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.