Answer the question
In order to leave comments, you need to log in
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
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;
}
}
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();
}
}
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 questionAsk a Question
731 491 924 answers to any question