Sort Rows In A Matrix In Java

To Sort Rows In-Place

Write a program to input integers into a 4 × 4 matrix (2-dimensional array).

Allow the user to input integers into it.

Now sort the individual rows in this matrix using exchange selection sort technique.

Example:
Original matrix:
22 14 23 25
81 26 31 10
58 64 17 12
55 33 26 14
Sorted matrix:
14 22 23 25
10 26 31 81
12 17 58 64
14 26 33 55

import java.io.*;
class SortEachRow{
    public static void main(String args[])throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int a[][] = new int[4][4];
        System.out.println("Enter array elements:");
        for(int i = 0; i < 4; i++){
            for(int j = 0; j < 4; j++)
                a[i][j] = Integer.parseInt(br.readLine());
        }
        System.out.println("Original matrix:");
        for(int i = 0; i < 4; i++){
            for(int j = 0; j < 4; j++){
                System.out.print(a[i][j] + "\t");
            }
            System.out.println();
        }
        for(int i = 0; i < 4; i++){
            for(int j = 0; j < 4; j++){
                int small = a[i][j];
                int pos = j;
                for(int k = j + 1; k < 4; k++){ 
                    if(small > a[i][k]){
                        small = a[i][k];
                        pos = k;
                    }
                }
                int temp = a[i][j];
                a[i][j] = a[i][pos];
                a[i][pos] = temp;

            }
        }
        System.out.println("Sorted matrix:");
        for(int i = 0; i < 4; i++){
            for(int j = 0; j < 4; j++){
                System.out.print(a[i][j] + "\t");
            }
            System.out.println();
        }
    }
}

To Sort Rows using a Separate Array

Write a program to allow the user to enter integers into a two-dimensional array of size [m × n].

The size of m and n are also entered by the user such that 2 < m < 10 and 2 < n < 10.

Now sort each row of the matrix using a separate one-dimensional array using the bubble sort technique.

Display the resultant matrix.

import java.io.*;
class SortRows{
    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 || n < 3 || m > 9 || n > 9){
            System.out.println("Size out of range!");
            return;
        }
        int a[][] = new int[m][n];
        System.out.println("Enter matrix elements:");
        for(int i = 0; i < m; i++){
            for(int j = 0; j < n; j++){
                a[i][j] = Integer.parseInt(br.readLine());
            }
            int t[] = new int[n];
            for(int j = 0; j < n; j++)
                t[j] = a[i][j];
            for(int p = 0; p < n; p++){
                for(int q = 0; q < n - 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 < n; j++)
                a[i][j] = 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();
        }
    }
}

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.