0% found this document useful (0 votes)
1 views

Program 21

imp

Uploaded by

Jaainam Jain
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Program 21

imp

Uploaded by

Jaainam Jain
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Program 21

Write a program to find matrix sorting in ascending order by using


bubble sort and display this new matrix

import java.util.*;
class Bubblesort
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int i,j,k=0,small,pos,temp;
System.out.print(“Enter the Number of rows :”);
int r=sc.nextInt();
System.out.print(“Enter the Number of columns :”);
int c=sc.nextInt();
int x[][]=new int[r][c];
int y[][]=new int[r][c];
int size=r*c;
int a[]=new int[size];
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
System.out.print(“Enter any number:”);
x[i][j]=sc.nextInt();
a[k++]=x[i][j];
}
}
for(i=0;i<size;i++)
{
for(j=0;j<size-i-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
System.out.println(“Original matrix”);
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
System.out.print(x[i][j]+”\t”);
}
System.out.println();
}
System.out.println(“Matrix after sorting”);
k=0;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
y[i][j]=a[k++];
System.out.print(y[i][j]+”\t”);
}
System.out.println();
}
}
}

Output :
Enter the Number of rows : 3
Enter the Number of columns : 3
Enter any number : 12
Enter any number : 90
Enter any number : 3
Enter any number : 7
Enter any number : 500
Enter any number : 300
Enter any number : 200
Enter any number : 700
Enter any number : 33
Original matrix :
12 90 3
7 500 300
200 700 33
Matrix after sorting
3 7 12
33 90 200
300 500 700

You might also like