From 06f945fa6effbe568aa943f0e2d7bc722f7f4577 Mon Sep 17 00:00:00 2001 From: sadiul-hakim <92100853+sadiul-hakim@users.noreply.github.com> Date: Mon, 3 Oct 2022 00:25:14 +0600 Subject: [PATCH 1/7] Search algorithm This algorithm is used to search a key from a sorted matrix. --- .../SearchInARowAndColWiseSortedMatrix.java | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 src/main/java/com/thealgorithms/searches/SearchInARowAndColWiseSortedMatrix.java diff --git a/src/main/java/com/thealgorithms/searches/SearchInARowAndColWiseSortedMatrix.java b/src/main/java/com/thealgorithms/searches/SearchInARowAndColWiseSortedMatrix.java new file mode 100644 index 000000000000..41f1906ab707 --- /dev/null +++ b/src/main/java/com/thealgorithms/searches/SearchInARowAndColWiseSortedMatrix.java @@ -0,0 +1,49 @@ +package com.thealgorithms.searches; + +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 void 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; + + while (i < n && j >= 0) { + + if (matrix[i][j] == value) { + System.out.println(value + " found at position row : " + i + " ,column :" + j); + return; + } + if (value > matrix[i][j]) { + + i++; + + } else { + j--; + } + + } + System.out.println(value + "Not Found."); + } + + public static void main(String[] args) { + 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 search = new SearchInARowAndColWiseSortedMatrix(); + search.search(matrix, 26); + } +} From fd2e7e8b1349dbe0c452dddb66727bee0f06f030 Mon Sep 17 00:00:00 2001 From: sadiul-hakim <92100853+sadiul-hakim@users.noreply.github.com> Date: Mon, 3 Oct 2022 00:27:11 +0600 Subject: [PATCH 2/7] Printing Matrix This algorithm helps printing a matrix in spiral order. --- .../others/PrintAMatrixInSpiralOrder.java | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 src/main/java/com/thealgorithms/others/PrintAMatrixInSpiralOrder.java diff --git a/src/main/java/com/thealgorithms/others/PrintAMatrixInSpiralOrder.java b/src/main/java/com/thealgorithms/others/PrintAMatrixInSpiralOrder.java new file mode 100644 index 000000000000..dbd6fb39e478 --- /dev/null +++ b/src/main/java/com/thealgorithms/others/PrintAMatrixInSpiralOrder.java @@ -0,0 +1,74 @@ +package com.thealgorithms.others; + +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 void 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; + + while (r < row && c < col) { + //print first row of matrix + for (i = c; i < col; i++) { + System.out.print(matrix[r][i] + " "); + + } + + //increase r by one because first row printed + r++; + + //print last column + for (i = r; i < row; i++) { + System.out.print(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--) { + System.out.print(matrix[row - 1][i] + " "); + } + + row--; + + } + + //print columns from first except printed elements + if (c < col) { + for (i = row - 1; i >= r; i--) { + System.out.print(matrix[i][c] + " "); + } + c++; + } + + } + + } + + public static void main(String[] args) { + 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 printer = new PrintAMatrixInSpiralOrder(); + printer.print(matrix, matrix.length, matrix[0].length); + + } +} From 40cc87c901274335a866ea8ada84460ff934a483 Mon Sep 17 00:00:00 2001 From: sadiul-hakim <92100853+sadiul-hakim@users.noreply.github.com> Date: Tue, 10 Jan 2023 01:39:16 +0600 Subject: [PATCH 3/7] Update SearchInARowAndColWiseSortedMatrix.java --- .../SearchInARowAndColWiseSortedMatrix.java | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/thealgorithms/searches/SearchInARowAndColWiseSortedMatrix.java b/src/main/java/com/thealgorithms/searches/SearchInARowAndColWiseSortedMatrix.java index 41f1906ab707..bc2952c476b2 100644 --- a/src/main/java/com/thealgorithms/searches/SearchInARowAndColWiseSortedMatrix.java +++ b/src/main/java/com/thealgorithms/searches/SearchInARowAndColWiseSortedMatrix.java @@ -1,5 +1,7 @@ package com.thealgorithms.searches; +import java.util.Arrays; + public class SearchInARowAndColWiseSortedMatrix { /** * Search a key in row and column wise sorted matrix @@ -9,18 +11,20 @@ public class SearchInARowAndColWiseSortedMatrix { * @author Sadiul Hakim : https://github.com/sadiul-hakim */ - public void search(int[][] matrix, int value) { + 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) { - System.out.println(value + " found at position row : " + i + " ,column :" + j); - return; + result[0] = i; + result[1] = j; + return result; } if (value > matrix[i][j]) { @@ -31,7 +35,7 @@ public void search(int[][] matrix, int value) { } } - System.out.println(value + "Not Found."); + return result; } public static void main(String[] args) { @@ -44,6 +48,7 @@ public static void main(String[] args) { }; var search = new SearchInARowAndColWiseSortedMatrix(); - search.search(matrix, 26); + int[] res = search.search(matrix, 26); + System.out.println(Arrays.toString(res)); } } From 58168e8e97af8ee2666a80e5052210e463b86d00 Mon Sep 17 00:00:00 2001 From: sadiul-hakim <92100853+sadiul-hakim@users.noreply.github.com> Date: Tue, 10 Jan 2023 01:40:21 +0600 Subject: [PATCH 4/7] Add files via upload --- ...estSearchInARowAndColWiseSortedMatrix.java | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 src/test/java/com/thealgorithms/searches/TestSearchInARowAndColWiseSortedMatrix.java diff --git a/src/test/java/com/thealgorithms/searches/TestSearchInARowAndColWiseSortedMatrix.java b/src/test/java/com/thealgorithms/searches/TestSearchInARowAndColWiseSortedMatrix.java new file mode 100644 index 000000000000..c5d37fcb37a0 --- /dev/null +++ b/src/test/java/com/thealgorithms/searches/TestSearchInARowAndColWiseSortedMatrix.java @@ -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); + } +} \ No newline at end of file From fc4d76dbb2c05908887cc597cc6253f7b193c2a8 Mon Sep 17 00:00:00 2001 From: Andrii Siriak Date: Tue, 10 Jan 2023 09:16:57 +0200 Subject: [PATCH 5/7] Apply suggestions from code review --- .../SearchInARowAndColWiseSortedMatrix.java | 17 ----------------- .../TestSearchInARowAndColWiseSortedMatrix.java | 2 +- 2 files changed, 1 insertion(+), 18 deletions(-) diff --git a/src/main/java/com/thealgorithms/searches/SearchInARowAndColWiseSortedMatrix.java b/src/main/java/com/thealgorithms/searches/SearchInARowAndColWiseSortedMatrix.java index bc2952c476b2..c92569cf1086 100644 --- a/src/main/java/com/thealgorithms/searches/SearchInARowAndColWiseSortedMatrix.java +++ b/src/main/java/com/thealgorithms/searches/SearchInARowAndColWiseSortedMatrix.java @@ -20,16 +20,13 @@ public int[] search(int[][] matrix, int value) { 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--; } @@ -37,18 +34,4 @@ public int[] search(int[][] matrix, int value) { } return result; } - - public static void main(String[] args) { - 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 search = new SearchInARowAndColWiseSortedMatrix(); - int[] res = search.search(matrix, 26); - System.out.println(Arrays.toString(res)); - } } diff --git a/src/test/java/com/thealgorithms/searches/TestSearchInARowAndColWiseSortedMatrix.java b/src/test/java/com/thealgorithms/searches/TestSearchInARowAndColWiseSortedMatrix.java index c5d37fcb37a0..0dcc6186fa9b 100644 --- a/src/test/java/com/thealgorithms/searches/TestSearchInARowAndColWiseSortedMatrix.java +++ b/src/test/java/com/thealgorithms/searches/TestSearchInARowAndColWiseSortedMatrix.java @@ -35,4 +35,4 @@ public void notFound() { int[] expectedResult = { -1, -1 }; assertArrayEquals(expectedResult, res); } -} \ No newline at end of file +} From 7a5102c649a502e9fefa903bfb8c0aa72b752961 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?sadiul-hakim=E2=98=AA=EF=B8=8F?= <92100853+sadiul-hakim@users.noreply.github.com> Date: Tue, 10 Jan 2023 14:30:31 +0600 Subject: [PATCH 6/7] Update PrintAMatrixInSpiralOrder.java --- .../others/PrintAMatrixInSpiralOrder.java | 47 ++++++++----------- 1 file changed, 19 insertions(+), 28 deletions(-) diff --git a/src/main/java/com/thealgorithms/others/PrintAMatrixInSpiralOrder.java b/src/main/java/com/thealgorithms/others/PrintAMatrixInSpiralOrder.java index dbd6fb39e478..d2065085d8c6 100644 --- a/src/main/java/com/thealgorithms/others/PrintAMatrixInSpiralOrder.java +++ b/src/main/java/com/thealgorithms/others/PrintAMatrixInSpiralOrder.java @@ -1,5 +1,8 @@ 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 @@ -10,65 +13,53 @@ public class PrintAMatrixInSpiralOrder { * @author Sadiul Hakim : https://github.com/sadiul-hakim */ - public void print(int[][] matrix, int row, int col) { + public List print(int[][] matrix, int row, int col) { - //r traverses matrix row wise from first + // r traverses matrix row wise from first int r = 0; - //c traverses matrix column wise from first + // c traverses matrix column wise from first int c = 0; int i; + List result = new ArrayList<>(); + while (r < row && c < col) { - //print first row of matrix + // print first row of matrix for (i = c; i < col; i++) { - System.out.print(matrix[r][i] + " "); - + result.add(matrix[r][i]); } - //increase r by one because first row printed + // increase r by one because first row printed r++; - //print last column + // print last column for (i = r; i < row; i++) { - System.out.print(matrix[i][col - 1] + " "); + result.add(matrix[i][col - 1]); } - //decrease col by one because last column has been printed + // decrease col by one because last column has been printed col--; - //print rows from last except printed elements + // print rows from last except printed elements if (r < row) { for (i = col - 1; i >= c; i--) { - System.out.print(matrix[row - 1][i] + " "); + result.add(matrix[row - 1][i]); } row--; } - //print columns from first except printed elements + // print columns from first except printed elements if (c < col) { for (i = row - 1; i >= r; i--) { - System.out.print(matrix[i][c] + " "); + result.add(matrix[i][c]); } c++; } } - + return result; } - public static void main(String[] args) { - 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 printer = new PrintAMatrixInSpiralOrder(); - printer.print(matrix, matrix.length, matrix[0].length); - - } } From 3447104d2e19432b4a6a161bff4d07a357cdbb69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?sadiul-hakim=E2=98=AA=EF=B8=8F?= <92100853+sadiul-hakim@users.noreply.github.com> Date: Tue, 10 Jan 2023 14:32:13 +0600 Subject: [PATCH 7/7] Add files via upload --- .../others/TestPrintMatrixInSpiralOrder.java | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 src/test/java/com/thealgorithms/others/TestPrintMatrixInSpiralOrder.java diff --git a/src/test/java/com/thealgorithms/others/TestPrintMatrixInSpiralOrder.java b/src/test/java/com/thealgorithms/others/TestPrintMatrixInSpiralOrder.java new file mode 100644 index 000000000000..867311e1bce1 --- /dev/null +++ b/src/test/java/com/thealgorithms/others/TestPrintMatrixInSpiralOrder.java @@ -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 res = printClass.print(matrix, matrix.length, matrix[0].length); + List 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 res = printClass.print(matrix, matrix.length, matrix[0].length); + List list = List.of(2, 2); + assertIterableEquals(res, list); + } +}