
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find Number of Sorted Rows in a Matrix in Java
Matrices are nothing but a collection of data elements arranged in a rectangular layout that is two-dimensional. In Java, an array with two dimensions can be considered as a matrix.
As per the problem statement the task is to count all the rows in a matrix that are sorted either in strictly increasing order or in strictly decreasing order.
Let's deep dive into this article, to know how it can be done by using Java programming language.
To show you some instances
Instance-1
Suppose the original matrix is{ { 5, 6, 7, 8 , { 5, 3, 1, 0 }, { 10, 7, 4, 3 }, { 13, 7, 8, 21 }, { 5, 4, 11 ,22 }, { 11, 2, 3 ,4 } };
After counting the sorted rows in a matrix, the result index will be:
The sorted rows in a matrix: 3
Instance-2
Suppose the original matrix is{ { 3, 5, 7, 9 }, { 5, 3, 1, 8 }, { 10, 7, 4, 3 }, { 4, 7, 8, 11 }, { 0, 4, 11 ,22 }, { 1, 2, 3 ,0 } };
After counting the sorted rows in a matrix, the result index will be:
The sorted rows in a matrix: 4
Algorithm
Step 1 ? Initialise and declare the matrix
Step 2 ?Check the increasing or decreasing order of the rows using a for loop.
Step 3 ? Count the total number of rows.
Step 4 ? Print the result.
Multiple Approaches
We have provided the solution in different approaches
By Using Static Initialization of Matrix
By Using User Defined Method
Let's see the program along with its output one by one.
Approach-1: By Using Static Initialization of Matrix
In this approach, matrix elements will be initialized in the program. Then as per the algorithm, find the number of sorted rows in a matrix.
Example
public class Main { public static void main(String arg[]){ int m = 6, n = 4; //Initialising and declaring the matrix int mat[][] = { { 5, 6, 7, 8 }, { 5, 3, 1, 0 }, { 10, 7, 4, 3 }, { 13, 7, 8, 21 }, { 5, 4, 11 ,22 }, { 11, 2, 3 ,4 } }; int result = 0; // counting from left to right side to count increasing order of rows for (int i = 0; i < m; i++) { //To check if there is any pair of element that are not in increasing order. int j; for (j = 0; j < n - 1; j++) if (mat[i][j + 1] <= mat[i][j]) break; //If the loop didn't break then all elements of current row were in increasing order if (j == n - 1) //count of increasing order result++; } // counting from right to left side to count decreasing order of rows for (int i = 0; i < m; i++) { //To check if there is any pair of elements that are not in decreasing order. int j; for (j = n - 1; j > 0; j--) if (mat[i][j - 1] <= mat[i][j]) break; if (n > 1 && j == 0) result++; } System.out.println("The sorted rows in a matrix: " + result); } }
Output
The sorted rows in a matrix: 3
Approach-2: By Using User Defined Method
In this approach, matrix elements will be initialized in the program. Then call a user defined method by passing the matrix as parameter and inside the method as per the algorithm find the number of sorted rows in a matrix.
Example
public class Main { public static void main(String arg[]){ //Initialising and declaring the matrix int m = 6, n = 4; int mat[][] = { { 3, 5, 7, 9 }, { 5, 3, 1, 8 }, { 10, 7, 4, 3 }, { 4, 7, 8, 11 }, { 0, 4, 11 ,22 }, { 1, 2, 3 ,0 } }; //calling user defined method sort(mat, m, n); } // user defined method static void sort(int mat[][], int r, int c){ //Initializing the result int result = 0; // counting from left to right side to count increasing order of rows for (int i = 0; i < r; i++) { //To check if there are any pairs of elements that are not in increasing order. int j; for (j = 0; j < c - 1; j++) if (mat[i][j + 1] <= mat[i][j]) break; //If the loop didn't break then all elements of current row were in increasing order if (j == c - 1) //count of increasing order result++; } // counting from right to left side to count decreasing order of rows for (int i = 0; i < r; i++) { //To check if there is any pair of elements that are not in decreasing order. int j; for (j = c - 1; j > 0; j--) if (mat[i][j - 1] <= mat[i][j]) break; //If the loop didn't break then all elements of current row were in decreasing order if (c > 1 && j == 0) //count of decreasing order result++; } //print the result System.out.println("The sorted rows in a matrix: " + result); } }
Ouput
The sorted rows in a matrix: 4
In this article, we explored different approaches to find the number of sorted rows in a matrix by using Java programming language.