0% found this document useful (0 votes)
29 views4 pages

Ascending Descending

Uploaded by

Saksham14
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views4 pages

Ascending Descending

Uploaded by

Saksham14
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Ques: - Write a program to input a 2d matrix of size m by n first Arrange row in

ascending order. Then arrange column in ascending order.


Example: -
ENTER THE VALUE OF M: 3
ENTER THE VALUE OF N: 3
ENTER ELEMENTS OF MATRIX:
ENTER ELEMENTS OF ROW 1: 1,2,3
ENTER ELEMENTS OF ROW 2: 4,5,6
ENTER ELEMENTS OF ROW 3: 7,8,9
ORIGINAL MATRIX
123
456
789
MATRIX AFTER SORTING ROWS
123
456
789
MATRIX AFTER SORTING BOTH ROWS IN ASCENDING AND COLOUMNS IN
DESCENDING
789
456
123
ALGORITHM
1. Start
2. Prompt the user to enter the values of M and N
3. Read the values of M and N
4. Create a 2D array 'a' of size MxN
5. Prompt the user to enter the elements of the matrix
6. Read the elements of the matrix into the array 'a'
7. Display "Original Matrix"
8. Loop through each row 'i' from 0 to M-1
a. Loop through each column 'j' from 0 to N-1
i. Print the value of a[i][j]
b. Print a new line
9. Sort each row of the matrix 'a' in ascending order
10. Display "Matrix After Sorting Rows"
11. Loop through each row 'i' from 0 to M-1
a. Loop through each column 'j' from 0 to N-1
i. Print the value of a[i][j]
b. Print a new line
12. Sort each column of the matrix 'a' in descending order
13. Display "Matrix After Sorting Rows in Ascending and Columns in Descending"
14. Loop through each row 'i' from 0 to M-1
a. Loop through each column 'j' from 0 to N-1
i. Print the value of a[i][j]
b. Print a new line
15. End
JAVA PROGRAM
import java.util.Scanner;

public class ArraySort


{
public static void main()
{
Scanner in = new Scanner(System.in);
System.out.print("ENTER THE VALUE OF M: ");
int m = in.nextInt();
System.out.print("ENTER THE VALUE OF N: ");
int n = in.nextInt();
int a[][] = new int[m][n];
System.out.println("ENTER ELEMENTS OF MATRIX:");
for (int i = 0; i < m; i++) {
System.out.println("ENTER ELEMENTS OF ROW " + (i+1) + ":");
for (int j = 0; j < n; j++) {
a[i][j] = in.nextInt();
}
}

System.out.println("ORIGINAL MATRIX");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
System.out.print(a[i][j] + " ");
}
System.out.println();
}

for (int i = 0; i < m; i++) {


for (int j = 0; j < n - 1; j++) {
for (int k = 0; k < n - j - 1; k++) {
if (a[i][k] > a[i][k + 1]) {
int t = a[i][k];
a[i][k] = a[i][k+1];
a[i][k+1] = t;
}
}
}
}

System.out.println("MATRIX AFTER SORTING ROWS");


for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
System.out.print(a[i][j] + " ");
}
System.out.println();
}

for (int i = 0; i < n; i++) {


for (int j = 0; j < m - 1; j++) {
for (int k = 0; k < m - j - 1; k++) {
if (a[k][i] < a[k + 1][i]) {
int temp = a[k][i];
a[k][i] = a[k + 1][i];
a[k + 1][i] = temp;
}
}
}
}

System.out.println("MATRIX AFTER SORTING BOTH ROWS IN ASCENDING AND COLOUMNS IN


DESCENDING");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
System.out.print(a[i][j] + " ");
}
System.out.println();
}
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy