Sort Matrix in Ascending Order Java
In this tutorial I am going to Sort Matrix in Ascending Order JAVA.
Checkout:- Selection Sort in Descending Order Java
Checkout:- Sum of Right Diagonal Elements of a Matrix
We are uploading these types of program. However, if you need fully compiled solution of any type of Java program then simply leave a comment below, we will help you at the earliest. 😊😊
import java.util.*;
public class Bubble_Sort_In_Matrix_Ascending_Order
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int n=0,temp,i,j;
System.out.println("Enter the number of Rows");
int r=sc.nextInt();
System.out.println("Enter the number of Columns");
int c=sc.nextInt();
int arr[][]=new int[r][c];
int brr[]=new int[r*c];
System.out.println("Enter the elements of Matrix");
for( i=0;i<r;i++)
{
for( j=0;j<c;j++)
{
arr[i][j]=sc.nextInt();
}
}
System.out.println("INPUT MATRIX IS:-");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
brr[n++]=arr[i][j];
System.out.print(arr[i][j]+" ");
}
System.out.println("");
}
for(int l=0;l<n-1;l++)
{
for(int k=0;k<n-1;k++)
{
if(brr[k]>brr[k+1])
{
temp=brr[k];
brr[k]=brr[k+1];
brr[k+1]=temp;
}
}
}
n=0;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
arr[i][j]=brr[n++];
}
}
System.out.println("REARRANGED MATRIX IS:-");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
System.out.print(arr[i][j]+" ");
}
System.out.println("");
}
}
}
------------
OUTPUT:-
-----------
Enter the number of Rows
4
Enter the number of Columns
4
Enter the elements of Matrix
1
5
6
3
2
4
5
6
9
8
7
5
3
2
6
8
INPUT MATRIX IS:-
1 5 6 3
2 4 5 6
9 8 7 5
3 2 6 8
REARRANGED MATRIX IS:-
1 2 2 3
3 4 5 5
5 6 6 6
7 8 8 9
Checkout:- Find Smallest and Largest Element from Square Matrix with Position
Checkout:- Bubble Sort In Descending Order Java
0 Comments
Just write down your question to get the answer...