[英]Java Sorting a 2d array rows into ascending and columns into descending order
Please, can someone help me, I need to sort my 2d array columns into descending order? 拜托,有人可以帮我吗,我需要按降序对2d数组列进行排序? I used this code to sort my array rows into ascending order but I now have to sort the columns into descending.
我使用此代码将数组行按升序排序,但现在必须将列按降序排序。
// 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();
}
}
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));
Just replace 只需更换
if (colArray[j - 1] < colArray[j]) {
...
}
with: 有:
if (colArray[j - 1] > colArray[j]) {
...
}
>
instead of <
>
而不是<
First of all your array is not 2D :) , just because each elemnt of array has a number to be called like Arr[0] and its value is eg 5 it doesnt mean its 2D. 首先,您的数组不是2D :),只是因为数组的每个元素都有一个像Arr [0]这样被调用的数字,并且它的值例如是5,但这并不意味着它是2D的。
but in order to sort your array into descending order you can use code below : 但是为了将数组降序排列,可以使用以下代码:
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;
}
}
}
I rewrote your sortSort
method to sort the columns. 我重写了您的
sortSort
方法以对列进行排序。 Though this shouldn't be the solution, but rather point you in the right direction. 尽管这不应该是解决方案,但可以为您指明正确的方向。
This is how I imagine a 2D-array: 这就是我想象的二维数组:
_ _ _ _ _
|_|_|_|_|_|
|_|_|_|_|_|
|_|_|_|_|_| <rows
^
cols
In code I would look at it this way: myArray[row][col]
在代码中,我会这样看:
myArray[row][col]
But more precisely its an array of arrays, so actually it looks more like this: 但更确切地说,它是一个数组数组,因此实际上看起来更像这样:
______________ ______________ ______________
| _ _ _ _ _ | _ _ _ _ _ | _ _ _ _ _ |
| |_|_|_|_|_| | |_|_|_|_|_| | |_|_|_|_|_| |
|______________|______________|______________|
^ ^ ^
row 0 row 1 row 2
Each row contains an array of columns. 每行包含一个列数组。 And that's the problem - you cannot compare the array of row 0 with the array of row 1 and say which array one is "greater" ... unless you sort it by it's size.
这就是问题所在-您无法将第0行的数组与第1行的数组进行比较,并说哪个数组“更大” ...除非按大小将其排序。
In your program you called your column sorting method like this: 在您的程序中,您这样调用了列排序方法:
//Print out sorted columns of array
for (int i = 0; i < sortArray.length; i++) {
sortSort(sortArray[i]);
}
The index i
iterates over the rows and calls sortSort
. 该指数
i
遍历行并调用sortSort
。 So for i=0
it takes the first row and passes the containing array of columns to sortSort
. 因此,对于
i=0
它采用第一行并将包含的列数组传递给sortSort
。 Thus your method can only sort the columns of a single row. 因此,您的方法只能对单个行的列进行排序。
sortSort(sortArray[i]); // sortArray[i] represents all columns of row i
To make it sort the columns, it must be aware of the whole 2D-array. 为了使其对列进行排序,它必须了解整个2D数组。 Columns and rows.
列和行。 Meaning you can only sort the whole array at once but not single columns.
这意味着您只能一次对整个数组排序,而不能对单个列排序。
Let's look at it in my example: 让我们在我的示例中看一下:
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;
}
}
}
}
}
You can test this code and see that it works. 您可以测试此代码,然后查看它是否有效。 However it can only work under certain circumstances!
但是,它只能在某些情况下工作!
The problem I mentioned earlier is that every row needs the exact same number of columns as the first: 我前面提到的问题是每一行都需要与第一行完全相同的列数:
This wouldn't work:
_ _ _ _
|_|_|_|_| <-- row 1 has 4 columns
|_|_|_|_ _ _ <-- row 2 only 3
|_|_|_|_|_|_| <-- row 3 has 6 columns
If you don't have to make this work for a variable number of columns - leave it be. 如果您不必针对可变的列数进行此操作,请保留它。 If you always have a nice table of n rows with each m columns, the above method does the trick.
如果您总是有一个漂亮的n行表,每行m列,那么上述方法就可以解决问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.