forked from sevastyan_b/SSPR_25
Создан второй сервис.
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
package ru.sguardian;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Random;
|
||||
|
||||
public class MatrixProcessor {
|
||||
public int[][] operationMatrix(int[][] array) {
|
||||
if (array.length <= 1) {
|
||||
return array;
|
||||
}
|
||||
|
||||
int mid = array.length / 2;
|
||||
int[][] left = Arrays.copyOfRange(array, 0, mid);
|
||||
int[][] right = Arrays.copyOfRange(array, mid, array.length);
|
||||
|
||||
left = operationMatrix(left);
|
||||
right = operationMatrix(right);
|
||||
|
||||
return merge(left, right);
|
||||
}
|
||||
|
||||
private int[][] merge(int[][] left, int[][] right) {
|
||||
int[][] result = new int[left.length + right.length][];
|
||||
int i = 0, j = 0, k = 0;
|
||||
|
||||
while (i < left.length && j < right.length) {
|
||||
if (findMaxElementRow(left[i]) >= findMaxElementRow(right[j])) {
|
||||
result[k++] = left[i++];
|
||||
} else {
|
||||
result[k++] = right[j++];
|
||||
}
|
||||
}
|
||||
|
||||
while (i < left.length) {
|
||||
result[k++] = left[i++];
|
||||
}
|
||||
while (j < right.length) {
|
||||
result[k++] = right[j++];
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static int findMaxElementRow(int[] row) {
|
||||
int max = Integer.MIN_VALUE;
|
||||
for (int num : row) {
|
||||
max = Math.max(max, num);
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
public int[][] generateMatrix(int size){
|
||||
int[][] array = new int[size][size];
|
||||
Random rand = new Random();
|
||||
for (int i = 0; i < size; i++){
|
||||
for (int j = 0; j < size; j++){
|
||||
array[i][j] = rand.nextInt(-1000, 1000);
|
||||
}
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public int[][] arraysMerge(int[][] arrayOne, int[][] arrayTwo){
|
||||
int[][] result = new int[arrayOne.length + arrayTwo.length][];
|
||||
System.arraycopy(arrayOne, 0, result, 0, arrayOne.length);
|
||||
System.arraycopy(arrayTwo, 0, result, arrayOne.length, arrayTwo.length);
|
||||
return result;
|
||||
}
|
||||
|
||||
public void printArray(int[][] array){
|
||||
for(int i = 0; i < array.length; i++){
|
||||
for (int j = 0; j < array.length; j++){
|
||||
System.out.print(array[i][j] + " ");
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user