Sort Matrix Column-wise in Java

Write a program to allow the user to enter integers into a two-dimensional array of size M × N.

The size of the rows and columns is also entered by the user.

Display the original matrix.

Now sort the integers column-wise in ascending order in the matrix, and display the resultant matrix.

Sorting in the matrix itself

import java.io.*;
class SortColumns{
    public static void main(String args[])throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Number of rows: ");
        int m = Integer.parseInt(br.readLine());
        System.out.print("Number of columns: ");
        int n = Integer.parseInt(br.readLine());
        int a[][] = new int[m][n];
        System.out.println("Enter array elements:");
        for(int i = 0; i < m; i++){
            for(int j = 0; j < n; j++){
                a[i][j] = Integer.parseInt(br.readLine());
            }
        }
        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] + "\t");
            }
            System.out.println();
        }
        for(int i = 0; i < n; i++){
            for(int j = 0; j < m; j++){
                for(int k = 0; k < m - 1 - j; 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("Sorted matrix:");
        for(int i = 0; i < m; i++){
            for(int j = 0; j < n; j++){
                System.out.print(a[i][j] + "\t");
            }
            System.out.println();
        }
    }
}

Sorting in a separate array

import java.io.*;
class SortColumns{
    public static void main(String args[])throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Number of rows: ");
        int m = Integer.parseInt(br.readLine());
        System.out.print("Number of columns: ");
        int n = Integer.parseInt(br.readLine());
        if(m < 3 || m > 9 || n < 3 || n > 9){
            System.out.println("Size out of range!");
            return;
        }
        int a[][] = new int[m][n];
        System.out.println("Enter array elements:");
        for(int i = 0; i < m; i++){
            for(int j = 0; j < n; j++){
                a[i][j] = Integer.parseInt(br.readLine());
            }
        }
        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] + "\t");
            }
            System.out.println();
        }
        for(int i = 0; i < n; i++){
            int t[] = new int[m];
            for(int j = 0; j < m; j++){
                t[j] = a[j][i];
            }
            for(int p = 0; p < t.length; p++){
                for(int q = 0; q < t.length - 1 - p; q++){ 
                    if(t[q] > t [q + 1]){
                        int temp = t[q];
                        t[q] = t[q + 1];
                        t[q + 1] = temp;
                    }
                }
            }
            for(int j = 0; j < m; j++){
                a[j][i] = t[j];
            }
        }
        System.out.println("Sorted matrix:");
        for(int i = 0; i < m; i++){
            for(int j = 0; j < n; j++){
                System.out.print(a[i][j] + "\t");
            }
            System.out.println();
        }
    }
}

5 thoughts on “Sort Matrix Column-wise in Java

  1. Sample Input & Output for the above program I sent is given:
    M = 4
    N = 3
    Enter elements of matrix:
    11
    -2
    3
    5
    16
    7
    9
    0
    4
    3
    1
    8

    Original Matrix:
    11 -2 3
    5 16 7
    9 0 4
    3 1 8
    Matrix after sorting columns:
    3 -2 3
    5 0 4
    9 1 7
    11 16 8
    How to solve the program by creating a single dimensional array and passing each elements in the column in a single dimensional array?

  2. Sir,a program is given which states that
    Write a program to allow the user to enter integers into a two-dimensional array of size M × N.

    The size of the rows and columns is also entered by the user.(M & N must be greater than 2 and less than 10)

    Display the original matrix.

    Now sort the integers column-wise in ascending order in the matrix, and display the resultant matrix.
    How to solve the program by creating a single dimensional array and passing each elements in the column in a single dimensional array?

Leave a Reply