Question
Program to sort boundary elements in ascending order in a matrix
Matrix = [9, 8, 7]
[6, 5, 4]
[3, 2, 1]
SORTED MATRIX
1 2 3
4 6 5
7 8 9
Share code with your friends
Share on whatsapp
Share on facebook
Share on twitter
Share on telegram
Code
Java
Python
Java
import java.util.Scanner;
public class BoundaryElementsOfMatrix
{
public static void main(String[] args)
{
int row=0,col=0,i=0,j=0,k=0,temp=0,count=0;
int arr[][],b[];
Scanner sc=new Scanner(System.in);
System.out.println("ENTER THE NUMBER OF ROW OF MATRIX");
row=sc.nextInt();
System.out.println("ENTER THE NUMBER OF COLUMN OF MATRIX");
col=sc.nextInt();
arr=new int[row][col];
System.out.println("ENTER THE ELEMENTS IN MATRIX");
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
arr[i][j]=sc.nextInt();
/*counting the number of boundary elements*/
if(i==0||i==(row-1)||j==0||j==(col-1))
{
count++;
}
}
}
b=new int[count];
System.out.println("ORIGINAL MATRIX");
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
if(i==0 ||j==0||i==(row-1)||j==(col-1))
{
b[k]=arr[i][j];
k++;
}
}
}
/*sorting one-dimensional array*/
for(i=0;i< b.length;i++)
{
for(j=0;j< b.length;j++)
{
if(b[i] < b[j])
{
temp=b[i];
b[i]=b[j];
b[j]=temp;
}
}
}
/*putting sorted elements to their positions*/
k=0;
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
if(i==0 ||j==0||i==(row-1)||j==(col-1))
{
arr[i][j]=b[k];
k++;
}
}
}
System.out.println("SORTED MATRIX");
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}
}
Python
import numpy as np
row=int(input("Enter number of rows:"))
columns=int(input("Enter number of columns:"))
matrix=np.empty([row,columns],dtype=np.int)
# Taking input from user
print("Enter elements in matrix:")
for i in range(0,row):
for j in range(0,columns):
print("(",(i+1),",",(j+1),")",":",end="")
matrix[i,j]=int(input())
#printing the matrix
print("Original Matrix:")
for i in range(0, row):
print(matrix[i])
#Finding boundary elements and putting them in a list
boundaryElements=[]
for i in range(0,row):
for j in range(0,columns):
if(i==0 or j==0 or i==(row-1) or j==(columns-1)):
boundaryElements.append(matrix[i,j])
for i in range(0,len(boundaryElements)):
for j in range(0, len(boundaryElements)):
if(boundaryElements[i]< boundaryElements[j]):
temp=boundaryElements[i]
boundaryElements[i]=boundaryElements[j]
boundaryElements[j]=temp
"""
You can also replace code from line 23 to line 28 with boundaryElements.sort()
it will do the same thing.
"""
k=0
for i in range(0,row):
for j in range(0,columns):
if(i==0 or j==0 or i==(row-1) or j==(columns-1)):
matrix[i,j]=boundaryElements[k]
k=k+1
print("Matrix with sorted boundary elements:")
for i in range(0, row):
print(matrix[i])
Coding Store
Sale

ISC QUESTION PAPERS WITH SOLUTION(PROGRAMMING ONLY)
Sale

ICSE QUESTION PAPER WITH SOLUTION(PROGRAMMING ONLY)
Sale

ISC QUESTION PAPERS WITH SOLUTION(PROGRAMMING ONLY)
Sale

ICSE QUESTION PAPER WITH SOLUTION(PROGRAMMING ONLY)
Sale

ISC QUESTION PAPERS WITH SOLUTION(PROGRAMMING ONLY)
Sale
