Q. Write a program to declare a square matrix A[][] of order (M × M)
where 'M' must be greater than 3 and less than 10. Allow the user to
input positive integers into this matrix and Sort non-boundary
elements of Matrix in Ascending Order using any sorting technique.
In this tutorial, we will learn how to sort non-boundary elements a matrix in ascending order.
In order to solve this solution you must be familiar with some basic concepts of matrix like its initialisation, finding transpose, sparse, symmetric, etc
Checkout:- Neon Number In Java - Solved Program
Checkout:- Sort Matrix in Ascending Order Java
Checkout:- Sorting Rows of Matrix in Descending Order Java
Checkout:-
Niven Or Harshad Number In Java
Checkout:- Spy Number In Java - Solved Program
Checkout:- Java Program to Add Two Matrices
Checkout:- Java Program to Subtract The Two Matrices
In this post, I have have written a bit lengthy bujt easy to understand java program below which sorts the non-boundary elements of matrix in ascending order.
Basically, the logic I have implemented in this program is pretty easy to understand.
First, I will ask the user to enter the size of the matrix then user will enter the elements of matrix after that I will access the non boundary elements and sort them in ascending order.
Program to Sort Non-Boundary Elements of Matrix in Ascending Order
import java.util.*;
class Sort_Matrix_In_Ascending_Order
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int arr[][]=new int[10][10];
int brr[]=new int[10*10];
int n=0;
System.out.print("INPUT:M=");
int M=sc.nextInt();
if(M<4||M>10)
System.out.println("THE MATRIX SIZE IS OUT OF RANGE");
else
for(int i=0;i<M;i++)
{
for(int j=0;j<M;j++)
{
System.out.print("Input a Element for Matrix:");
arr[i][j]=sc.nextInt();
}
}
System.out.println("OUTPUT:\nORIGINAL MATRIX");
for(int i=0;i<M;i++)
{
for(int j=0;j<M;j++)
{
System.out.print(arr[i][j]+"\t");
if(i!=0 && j!=0 && i!=M-1 && j!=M-1)
{
brr[n++]=arr[i][j];
}
}
System.out.println(" ");
}
for(int i=0;i<n-1;i++)
{
int smallest=brr[i];
int pos=i;
for(int j=i+1;j<n;j++)
{
if(brr[j]<smallest)
{
smallest=brr[j];
pos=j;
}
}
brr[pos]=brr[i];
brr[i]=smallest;
}
n=0;
for(int i=0;i<M;i++)
{
for(int j=0;j<M;j++)
{
if(i!=0 && j!=0 && i!=M-1 && j!=M-1)
{
arr[i][j]=brr[n++];
}
}
}
System.out.println("REARRANGED MATRIX");
for(int i=0;i<M;i++)
{
for(int j=0;j<M;j++)
{
System.out.print(arr[i][j]+"\t");
}
System.out.println(" ");
}
}
}
-----------
OUTPUT:-
-----------
INPUT:M=4
Input a Element for Matrix:1
Input a Element for Matrix:4
Input a Element for Matrix:5
Input a Element for Matrix:2
Input a Element for Matrix:3
Input a Element for Matrix:6
Input a Element for Matrix:7
Input a Element for Matrix:8
Input a Element for Matrix:9
Input a Element for Matrix:6
Input a Element for Matrix:5
Input a Element for Matrix:4
Input a Element for Matrix:1
Input a Element for Matrix:2
Input a Element for Matrix:3
Input a Element for Matrix:5
OUTPUT:
ORIGINAL MATRIX:
1 4 5 2
3 6 7 8
9 6 5 4
1 2 3 5
REARRANGED MATRIX:
1 4 5 2
3 5 6 8
9 6 7 4
1 2 3 5
-----------------
0 Comments
Just write down your question to get the answer...