Home > Software engineering >  How do I sort a matrix by sum of negative odd numbers?
How do I sort a matrix by sum of negative odd numbers?

Time:09-22

I`m stuck on that problem. I need to calculate sum of negative odd numbers in every column in matrix and sort it in ascending order. I wrote all methods to create/fill/print matrix, but i dunno how to implement sorting algorithm.

import java.util.Arrays;
import java.util.Collections;
import java.util.Random;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int size = scanner.nextInt();
        int[][] array = Main.fillMatrix(size);
        Main.printMatrix(array);
        Main.sortMatrix(array);
        Main.printMatrix(array);

    }

    public static int getRandomNumberUsingNextInt(int min, int max) {
        Random random = new Random();
        return random.nextInt(max - min)   min;
    }

    public static void printMatrix(int[][] matrix){
        for (int i = 0; i < matrix.length; i  ) {
            for (int j = 0; j < matrix.length; j  ) {
                System.out.print(matrix[i][j]   " ");
            }
            System.out.println("");
        }
    }

    public static int[][] fillMatrix(int size) {
        int[][] matrix = new int[size][size];
        for (int i = 0; i < size; i  ) {
            for (int j = 0; j < size; j  ) {
                matrix[i][j] = Main.getRandomNumberUsingNextInt(-10, 10);
            }
        }
        return matrix;
    }

    static void sortMatrix(int[][] matrix) {
        
    }
}

CodePudding user response:

Since the matrix is square and needs to be sorted by column, it may be better resolved like this:

  1. Transpose the input matrix (change columns to rows)
static void transpose(int[][] arr) {
    for (int i = 0, n = arr.length; i < n; i  ) {
        for (int j = i   1; j < n; j  ) {
            int t = arr[i][j];
            arr[i][j] = arr[j][i];
            arr[j][i] = t;
        }
    }
}
  1. Sort the transposed matrix by rows using Comparator::comparingInt along with a function calculating the total of negative odd numbers
public static int sumNegOdd(int[] row) {
    return Arrays.stream(row).filter(x -> x % 2 == -1).sum();
}
  1. Transpose the sorted matrix once again and restore rows to columns
public static int[][] sort(int[][] arr) {
    transpose(arr);
    
    int[][] res = Arrays.stream(arr)
                        .sorted(Comparator.comparingInt(MyClass::sumNegOdd))
                        .toArray(int[][]::new);
    transpose(res);

    return res;
}

Test:

int[][] arr = makeMatrix(5);
System.out.println("----\nbefore");
printMatrix(arr);
arr = sort(arr);
    
System.out.println("----\nafter");
printMatrix(arr);

Output:

----
before
-7  -3   0  -8   6  
-8   5   0  -2  -9  
 0   9  -7  -7   0  
-1  -6   9  -1   9  
 8   0  -9  -2   0  
----
after
 0   6  -7  -8  -3  
 0  -9  -8  -2   5  
-7   0   0  -7   9  
 9   9  -1  -1  -6  
-9   0   8  -2   0

Additional utility methods:

static int[][] makeMatrix(int n) {
    Random r = new Random();
    return IntStream.range(0, n)
                    .mapToObj(i -> r.ints(n, -9, 10).toArray())
                    .toArray(int[][]::new);
}

static void printMatrix(int[][] arr) {
    for (int i = 0; i < arr.length; i  ) {
        for (int j = 0; j < arr[i].length; j  ) {
            System.out.printf("% 2d\t", arr[i][j]);
        }
        System.out.println();
    }
}
  • Related