Saturday, February 9, 2019

To sort a double dimensional array(ISC 2018)

The program given here is the exact answer of the question that was asked in ISC practical 2018
question number 2.
Qns-create a double dimensional  Integer type A[][] array  of order M*N.
1.take the input from user
2.display the content of array
3.sort every row of the array
 
sorting technique that is used selection sort

import java.util.*;
class mat1
{
public static void main(String[] args)
{
System.out.println("Hello World!");

Scanner in = new Scanner(System.in);
     int M,N,i,j,x;
System.out.println("enter number of rows and columns");
M=in.nextInt();
N=in.nextInt();
int A[][]=new int[M][N];
for(i=0;i<M;i++)
{
for(j=0;j<N;j++)
{
A[i][j]=in.nextInt();
}

}
for(i=0;i<M;i++)// diplay matrix
{
for(j=0;j<N;j++)
{
System.out.print(" "+A[i][j]);
}
        System.out.println();
}
int min,swap;
for(i=0;i<M;i++)// to focus on one row at a time
{
for(j=0;j<N;j++)
{
min=j;//to make comparison
for(x=j;x<N;x++)
{
if(A[i][min]>A[i][x])
{
min=x;//finding the smallest number in each row
}

}
swap=A[i][j];
A[i][j]=A[i][min];
A[i][min]=swap;
}
}
for(i=0;i<M;i++)
{
for(j=0;j<N;j++)
{
System.out.print(" "+A[i][j]);
}
       System.out.println();
}
}}

No comments:

Post a Comment