
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
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.
- Related Articles
- Find the Kth Smallest Sum of a Matrix With Sorted Rows in C++
- Count all sorted rows in a matrix in C++
- Find a common element in all rows of a given row-wise sorted matrix in C++
- How to find mean for x number of rows in a column in an R matrix?
- Find duplicate rows in a binary matrix in C++
- Find median in row wise sorted matrix in C++
- Finding the number of rows and columns in a given matrix using Numpy
- How to divide matrix rows by number of columns in R?
- Find number of cavities in a matrix in C++
- Find distinct elements common to all rows of a matrix in Python
- Find distinct elements common to all rows of a Matrix in C++
- Find Scalar Multiplication of a Matrix in Java?
- How to remove rows in R matrix that contains a specific number?
- JavaScript Program to Find median in row wise sorted matrix
- Java Program to Interchange Elements of First and Last in a Matrix Across Rows
