mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-14 01:02:03 +08:00
Add two algorithms with matrixes (#3364)
This commit is contained in:
@ -0,0 +1,65 @@
|
|||||||
|
package com.thealgorithms.others;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class PrintAMatrixInSpiralOrder {
|
||||||
|
/**
|
||||||
|
* Search a key in row and column wise sorted matrix
|
||||||
|
*
|
||||||
|
* @param matrix matrix to be searched
|
||||||
|
* @param row number of rows matrix has
|
||||||
|
* @param col number of columns matrix has
|
||||||
|
* @author Sadiul Hakim : https://github.com/sadiul-hakim
|
||||||
|
*/
|
||||||
|
|
||||||
|
public List<Integer> print(int[][] matrix, int row, int col) {
|
||||||
|
|
||||||
|
// r traverses matrix row wise from first
|
||||||
|
int r = 0;
|
||||||
|
// c traverses matrix column wise from first
|
||||||
|
int c = 0;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
List<Integer> result = new ArrayList<>();
|
||||||
|
|
||||||
|
while (r < row && c < col) {
|
||||||
|
// print first row of matrix
|
||||||
|
for (i = c; i < col; i++) {
|
||||||
|
result.add(matrix[r][i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// increase r by one because first row printed
|
||||||
|
r++;
|
||||||
|
|
||||||
|
// print last column
|
||||||
|
for (i = r; i < row; i++) {
|
||||||
|
result.add(matrix[i][col - 1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// decrease col by one because last column has been printed
|
||||||
|
col--;
|
||||||
|
|
||||||
|
// print rows from last except printed elements
|
||||||
|
if (r < row) {
|
||||||
|
for (i = col - 1; i >= c; i--) {
|
||||||
|
result.add(matrix[row - 1][i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
row--;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// print columns from first except printed elements
|
||||||
|
if (c < col) {
|
||||||
|
for (i = row - 1; i >= r; i--) {
|
||||||
|
result.add(matrix[i][c]);
|
||||||
|
}
|
||||||
|
c++;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,37 @@
|
|||||||
|
package com.thealgorithms.searches;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
public class SearchInARowAndColWiseSortedMatrix {
|
||||||
|
/**
|
||||||
|
* Search a key in row and column wise sorted matrix
|
||||||
|
*
|
||||||
|
* @param matrix matrix to be searched
|
||||||
|
* @param value Key being searched for
|
||||||
|
* @author Sadiul Hakim : https://github.com/sadiul-hakim
|
||||||
|
*/
|
||||||
|
|
||||||
|
public int[] search(int[][] matrix, int value) {
|
||||||
|
int n = matrix.length;
|
||||||
|
// This variable iterates over rows
|
||||||
|
int i = 0;
|
||||||
|
// This variable iterates over columns
|
||||||
|
int j = n - 1;
|
||||||
|
int[] result = { -1, -1 };
|
||||||
|
|
||||||
|
while (i < n && j >= 0) {
|
||||||
|
if (matrix[i][j] == value) {
|
||||||
|
result[0] = i;
|
||||||
|
result[1] = j;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
if (value > matrix[i][j]) {
|
||||||
|
i++;
|
||||||
|
} else {
|
||||||
|
j--;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,35 @@
|
|||||||
|
package com.thealgorithms.others;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
public class TestPrintMatrixInSpiralOrder {
|
||||||
|
@Test
|
||||||
|
public void testOne() {
|
||||||
|
int[][] matrix = {
|
||||||
|
{ 3, 4, 5, 6, 7 },
|
||||||
|
{ 8, 9, 10, 11, 12 },
|
||||||
|
{ 14, 15, 16, 17, 18 },
|
||||||
|
{ 23, 24, 25, 26, 27 },
|
||||||
|
{ 30, 31, 32, 33, 34 }
|
||||||
|
};
|
||||||
|
var printClass = new PrintAMatrixInSpiralOrder();
|
||||||
|
List<Integer> res = printClass.print(matrix, matrix.length, matrix[0].length);
|
||||||
|
List<Integer> list = List.of(3, 4, 5, 6, 7, 12, 18, 27, 34, 33, 32, 31, 30, 23, 14, 8, 9, 10, 11, 17, 26, 25,
|
||||||
|
24, 15, 16);
|
||||||
|
assertIterableEquals(res, list);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testTwo() {
|
||||||
|
int[][] matrix = {
|
||||||
|
{ 2, 2 }
|
||||||
|
};
|
||||||
|
var printClass = new PrintAMatrixInSpiralOrder();
|
||||||
|
List<Integer> res = printClass.print(matrix, matrix.length, matrix[0].length);
|
||||||
|
List<Integer> list = List.of(2, 2);
|
||||||
|
assertIterableEquals(res, list);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,38 @@
|
|||||||
|
package com.thealgorithms.searches;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
public class TestSearchInARowAndColWiseSortedMatrix {
|
||||||
|
@Test
|
||||||
|
public void searchItem() {
|
||||||
|
int[][] matrix = {
|
||||||
|
{ 3, 4, 5, 6, 7 },
|
||||||
|
{ 8, 9, 10, 11, 12 },
|
||||||
|
{ 14, 15, 16, 17, 18 },
|
||||||
|
{ 23, 24, 25, 26, 27 },
|
||||||
|
{ 30, 31, 32, 33, 34 }
|
||||||
|
};
|
||||||
|
|
||||||
|
var test = new SearchInARowAndColWiseSortedMatrix();
|
||||||
|
int[] res = test.search(matrix, 16);
|
||||||
|
int[] expectedResult = { 2, 2 };
|
||||||
|
assertArrayEquals(expectedResult, res);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void notFound() {
|
||||||
|
int[][] matrix = {
|
||||||
|
{ 3, 4, 5, 6, 7 },
|
||||||
|
{ 8, 9, 10, 11, 12 },
|
||||||
|
{ 14, 15, 16, 17, 18 },
|
||||||
|
{ 23, 24, 25, 26, 27 },
|
||||||
|
{ 30, 31, 32, 33, 34 }
|
||||||
|
};
|
||||||
|
|
||||||
|
var test = new SearchInARowAndColWiseSortedMatrix();
|
||||||
|
int[] res = test.search(matrix, 96);
|
||||||
|
int[] expectedResult = { -1, -1 };
|
||||||
|
assertArrayEquals(expectedResult, res);
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user