Program 22
Program 22
(b) Find the maximum and minimum value in the matrix and display them.
(c) Sort the elements of the matrix in ascending order using any standard
sorting technique and rearrange them in the matrix.
Method 2
import java.util.*;
class Sorting
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int i,j,k=0,big,small,pos,temp,row=0,col=0,row1=0,col1=0;;
System.out.print("Enter the M :");
int M=sc.nextInt();
if(M>2 && M<20)
{
int x[][]=new int[M][M];
int y[][]=new int[M][M];
int size=M*M;
int a[]=new int[size];
for(i=0;i<M;i++)
{
for(j=0;j<M;j++)
{
System.out.print("Enter any number:");
x[i][j]=sc.nextInt();
a[k++]=x[i][j];
}
}
for(i=0;i<size;i++)
{
for(j=0;j<size-i-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
System.out.println("Original matrix");
for(i=0;i<M;i++)
{
for(j=0;j<M;j++)
{
System.out.print(x[i][j]+"\t");
}
System.out.println();
}
big=x[0][0];
small=x[0][0];
for(i=0;i<M;i++)
{
for(j=0;j<M;j++)
{
if(x[i][j]>big)
{
big=x[i][j];
row=i;
col=j;
}
if(x[i][j]<small)
{
small=x[i][j];
row1=i;
col1=j;
}
}
}
System.out.println("Largest number"+big);
System.out.println("Row="+row);
System.out.println("Column="+col);
System.out.println("Smallest number"+small);
System.out.println("Row="+row1);
System.out.println("Column="+col1);
System.out.println("Matrix after sorting");
k=0;
for(i=0;i<M;i++)
{
for(j=0;j<M;j++)
{
y[i][j]=a[k++];
System.out.print(y[i][j]+"\t");
}
System.out.println();
}
}
else
{
System.out.println("Size out of Range");
}
}
}