请,有人可以帮我吗,我需要将2d数组列按降序排列?我使用此代码将数组行按升序排序,但现在必须将列按降序排序。
// Initialize array
static int[][] sortArray = new int[7][7];
public static void main(String args[]) {
//initialize array values with random numbers
for (int i = 0; i < sortArray.length; i++) {
for (int j = 0; j < sortArray[i].length; j++) {
sortArray[i][j] = (int) (Math.random() * 100);
}
}
System.out.println("\n" + "Before sorting");
displayArray();
//Print out sorted array
for (int i = 0; i < sortArray.length; i++) {
bubbleSort(sortArray[i]);
}
System.out.println("\n" + "Array rows in ascending order");
displayArray();
//Print out sorted columns of array
for (int i = 0; i < sortArray.length; i++) {
sortSort(sortArray[i]);
}
System.out.println("\n" + "Array column in descending order");
displayArray();
}
// Sort rows into ascending order
public static void bubbleSort(int[] numArray) {
int n = numArray.length;
int temp = 0;
for (int i = 0; i < n; i++) {
for (int j = 1; j < (n - i); j++) {
if (numArray[j - 1] > numArray[j]) {
temp = numArray[j - 1];
numArray[j - 1] = numArray[j];
numArray[j] = temp;
}
}
}
}
//Sort cols into descending order
public static void sortSort(int[] colArray) {
int n = colArray.length;
int temp = 0;
for (int i = 0; i < n; i++) {
for (int j = 1; j < (n - i); j++) {
if (colArray[j - 1] < colArray[j]) {
temp = colArray[j - 1];
colArray[j - 1] = colArray[j];
colArray[j] = temp;
}
}
}
}
// Print out arrays
private static void displayArray() {
int i, j = 0;
System.out.println("-------------------------------------");
System.out.println(" ");
for (i = 0; i < sortArray.length; i++) {
for (j = 0; j < sortArray[i].length; j++) {
System.out.print(sortArray[i][j] + "\t" + "\t");
}
System.out.println();
}
}
答案 0 :(得分:1)
int[] colArray = new int[] { 2, 9, 4, 5};
int n = colArray.length;
int temp = 0;
for (int i = 0; i < n; i++) {
for (int j = 1; j < (n - i); j++) {
if (colArray[j - 1] > colArray[j]) {
temp = colArray[j - 1];
colArray[j - 1] = colArray[j];
colArray[j] = temp;
}
}
}
Arrays.stream(colArray).forEach(data -> System.out.println(data));
答案 1 :(得分:1)
只需更换
if (colArray[j - 1] < colArray[j]) {
...
}
具有:
if (colArray[j - 1] > colArray[j]) {
...
}
>
而不是<
答案 2 :(得分:0)
首先,您的数组不是2D :),只是因为数组的每个元素都有一个像Arr [0]一样被调用的数字,并且它的值例如是5,但这并不意味着它是2D的。
但是为了将数组降序排列,可以使用以下代码:
int n = intArray.length;
int temp = 0;
for(int i=0; i < n; i++){
for(int j=1; j < (n-i); j++){
if(intArray[j-1] < intArray[j]){
//swap the elements!
temp = intArray[j-1];
intArray[j-1] = intArray[j];
intArray[j] = temp;
}
}
}
答案 3 :(得分:0)
我重写了您的sortSort
方法以对列进行排序。尽管这不应该是解决方案,但可以为您指明正确的方向。
这就是我想象的二维数组:
_ _ _ _ _
|_|_|_|_|_|
|_|_|_|_|_|
|_|_|_|_|_| <rows
^
cols
在代码中,我会这样看:myArray[row][col]
但更确切地说,它是一个数组数组,因此实际上看起来更像这样:
______________ ______________ ______________
| _ _ _ _ _ | _ _ _ _ _ | _ _ _ _ _ |
| |_|_|_|_|_| | |_|_|_|_|_| | |_|_|_|_|_| |
|______________|______________|______________|
^ ^ ^
row 0 row 1 row 2
每行包含一个列数组。这就是问题所在-您无法将第0行的数组与第1行的数组进行比较,并且不能说第一个数组“更大” ...除非按大小将其排序。
在您的程序中,您像这样调用了列排序方法:
//Print out sorted columns of array
for (int i = 0; i < sortArray.length; i++) {
sortSort(sortArray[i]);
}
索引i
遍历行并调用sortSort
。因此,对于i=0
,它采用第一行并将包含的列数组传递给sortSort
。因此,您的方法只能对单个行的列进行排序。
sortSort(sortArray[i]); // sortArray[i] represents all columns of row i
要使其对列进行排序,必须知道整个2D数组。列和行。这意味着您只能一次对整个数组进行排序,而不能对单个列进行排序。
让我们看看我的示例:
public static void sortSort(int[][] colArray) { // we pass the whole 2D-array int[rows][cols]
int n = colArray.length;
int temp = 0;
// since every row contains a whole array, you cannot really sort the row itself.
// But you can sort it by its columns value (confusing, I know - sorry)
// we take the first row as reference (colArray[0])
// and iterate over every column (from 0 to colArray[0].length)
// that's a problem, but we get to that later
for (int col = 0; col < colArray[0].length; col ++) {
// your sorting loops are working, so we don't change them
for (int i = 0; i < n; i++) {
for (int j = 1; j < (n - i); j++) {
// and here is the magic we compare row j with row j-1 of the current column (col)
if (colArray[j - 1][col] < colArray[j][col]) {
temp = colArray[j - 1][col];
colArray[j - 1][col] = colArray[j][col];
colArray[j][col] = temp;
}
}
}
}
}
您可以测试此代码,然后查看它是否有效。但是,它只能在某些情况下工作!
我前面提到的问题是每一行都需要与第一行完全相同的列数:
This wouldn't work:
_ _ _ _
|_|_|_|_| <-- row 1 has 4 columns
|_|_|_|_ _ _ <-- row 2 only 3
|_|_|_|_|_|_| <-- row 3 has 6 columns
如果您不必针对可变的列数进行此操作,请保留它。如果您总是有一个漂亮的n行表,每行m列,那么上述方法就可以解决问题。