← All resources

Wrtite a program to input a 2-Dimensional array and sort each row in ascending order

<h3>Output</h3>
Enter the size of the array:
3
4
Enter the values for the array:
11
54
10
4
5
7
2
88
12
9
65
29
The array before sorting:
11	54	10	4
5	7	2	88
12	9	65	29
The Sorted array:
4	10	11	54
2	5	7	88
9	12	29	65
import java.io.*;
public class array2D_rowsort
{
    public static void main()throws IOException
    {
        BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
        int a[][], m, n, i, j, tmp, k;
        System.out.println("Enter the size of the array:");
        m = Integer.parseInt(r.readLine());
        n = Integer.parseInt(r.readLine());
        a = new int[m][n];
        System.out.println("Enter the values for the array:");
        for(i=0;i < m;i++)
        {
            for(j=0;j < n;j++)
            {
                a[i][j] = Integer.parseInt(r.readLine());
            }
        }
        for(k=0;k < m;k++)
        {
            for(i=0;i < n;i++)
            {
                for(j=0;j < n-1-i;j++)
                {
                    if(a[k][j]>a[k][j+1])
                    {
                        tmp = a[k][j];
                        a[k][j] = a[k][j+1];
                        a[k][j+1] = tmp;
                    }
                }
            }
        }
        System.out.println("The rows of the array in sorted order:");
        for(i=0;i < m;i++)
        {
            for(j=0;j < n;j++)
            {
                System.out.print(a[i][j]+"t");
            }
            System.out.println();
        }
    }
}