A
A
AlexMark2016-09-06 15:59:36
Java
AlexMark, 2016-09-06 15:59:36

Sorting a Matrix (Two Dimensional Array) Java?

I asked myself a question (new to Jave, just solving problems for the initial levels) what would be better for sorting a matrix, overtaking it into an array, sorting it, and then back into a matrix, or sorting the matrix right away?
How can I check in Idea which solution works faster and allocates less memory per process?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Evhen, 2016-09-06
@EugeneP2

If you are planning to sort a matrix, then of course it would be more efficient to store it as a one-dimensional array so that you can sort it efficiently using the quick sort implemented by the sort method in the java.util.Arrays class.
And so that you can work with an array as with a matrix, you can implement an adapter class

import java.util.Arrays;

public class IntMatrix {
    private final int[] lineMatrix;
    private final int n;
    private final int m;

    public IntMatrix(int[] lineMatrix, int n, int m) {

      if (n < 0 || m < 0 || lineMatrix.length != n * m)
        throw new IllegalArgumentException();

      this.lineMatrix = lineMatrix.clone();
      this.n = n;
      this.m = m;
    }

    public int get(int i, int j) {
      if (i < n && j < m)
        return lineMatrix[i * m + j];
      else
        throw new ArrayIndexOutOfBoundsException();
    }

    public void set(int value, int i, int j) {
      if (i < n && j < m)
        lineMatrix[i * m + j] = value;
      else
        throw new ArrayIndexOutOfBoundsException();
    }

    public  void sort() {
      // готовая реализация быстрой сортировки
      Arrays.sort(lineMatrix);
    }

    public int getN() {
      return n;
    }

    public int getM() {
      return m;
    }
  }

Usage example
public static void main(String[] args) {


    int n = 10, m = 15;

    IntMatrix intMatrix = new IntMatrix(new int[n * m], n, m);

    fillMatrixByRandomValues(intMatrix);

    printMatrix(intMatrix);

    intMatrix.sort();

    System.out.println("---------------");

    printMatrix(intMatrix);
  }


  /** заполнение матрицы случайными числами */
  static void fillMatrixByRandomValues(IntMatrix matrix) {
    Random random = new Random();
    for (int i = 0; i < matrix.getN(); i++) {
      for (int j = 0; j < matrix.getM(); j++) {
        matrix.set(random.nextInt(1000), i, j);
      }
    }
  }

  /** печать матрицы в консоль */
  static void printMatrix(IntMatrix matrix) {
    for (int i = 0; i < matrix.getN(); i++) {
      for (int j = 0; j < matrix.getM(); j++) {
        System.out.printf("%s\t", matrix.get(i , j));
      }
      System.out.println();
    }
  }

A linear array will take up less space than a two-dimensional array. The sorting implemented in the java.util.Arrays class is good both in terms of speed and memory consumption.

K
Konstantin Stepanov, 2016-09-06
@koronabora

The matrix is ​​stored as a two-dimensional array or list. That's how we work with him. You can expand the matrix as a linear array. but here you have to have some fun with indexes, IMHO, it's easier to work with a two-dimensional array.
Measurements are made by "spotting" the running time of the algorithm. We record the readings of the system time before and after sorting. The difference is the execution time of the algorithm. We run it several times, take the average value of the time. Do not forget to disable all unnecessary programs on the computer.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question