How to print the values โ€‹โ€‹of a 2D array in descending order?

So, I have a 2D matrix, and I'm trying to print the values โ€‹โ€‹from the largest to the smallest. I basically do this, always looking for max, and when I find it, I set this position equal 1to adjacencyMatrixso that we don't recount it again. The problem is that when I checked the code that it started correctly, printed the largest, and then skipped the second largest. Then he found the 3rd and 4th largest. We skipped a few more, and then, finally, we just started typing 0 s.

Here is my code:

public static void findLongestPath(int rows, int columns, int[][] elevationMatrix, int[][] adjacencyMatrix)
    {
        int max = 0;

        for (int x = 0; x < rows * columns; x++)
        {   
            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < columns; j++)
                {
                    if (elevationMatrix[i][j] > max && adjacencyMatrix[i][j] == 0)
                    {
                        max = elevationMatrix[i][j];
                        adjacencyMatrix[i][j] = 1;
                    }
                }
            }

            System.out.println(max);
            max = 0;
        }
    }

I looked at it for a while and cannot find a mistake, therefore, although another pair of eyes can help.

P.S. , , , , . .

+4
3
public static void findLongestPath(int rows, int columns, int[][] elevationMatrix, int[][] adjacencyMatrix)
    {
        int max = 0;
        int cX, cY;

        for (int x = 0; x < rows * columns; x++)
        {   
            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < columns; j++)
                {
                    if (elevationMatrix[i][j] > max && adjacencyMatrix[i][j] == 0)
                    {
                        max = elevationMatrix[i][j]; // possible max, xth iteration
                        cX = i; // store i
                        cY = j; // store j
                    }
                }
            }

            System.out.println(max); // global max, xth iteration
            max = 0;
            // cX and cJ now point to coordinates of global max
            // all the possible max value coordinates are ignored.
            adjacencyMatrix[cX][cJ] = 1;
        }
    }

, adjacencyMatrix[][] = 1, ( ), , max ( ).

, , .

+1
public static void findLongestPath(int rows, int columns, int[][] elevationMatrix)
{
   class PosAndValue implements Comparable<PosAndValue> {
     final int x;
     final int y;
     final int value;
     PosAndValue(int x, int y, int value) {
       this.x = x;
       this.y = y;
       this.value = value;
     }
     public int compareTo(PosAndValue other) {
       return Integer.compare(value, other.value);
     }
   }
   PosAndValue[] array = new PosAndValue[rows * columns];
   for (int i = 0; i < rows; i++) {
     for (int j = 0; j < columns; j++) {
       array[i * columns + j] = new PosAndValue(i, j, elevationMatrix[i][j]);
     }
   }
   Arrays.sort(array);
   for (int i = array.length - 1; i >= 0; i--) {
     System.out.println(array[i].value);
   }
}
0

, .

1D . temp , .

~ 10 : temp, , .

int[] tempArr = new int[rows * columns];
for(int i = 0; i < rows; i++){
    for(int j = 0; j < columns; j++){
        tempArr[(i * col) + j] = elevationMatrix[i][j];
    }
}
Arrays.sort(tempArr);
for(int x = (rows * columns) - 1; x >= 0; x--){
    System.out.println(tempArr[x]);
}
0

Source: https://habr.com/ru/post/1657155/


All Articles