#include<iostream>
using namespace std;
#include<conio.h>
int main()
{
int a[20][20],n,b[20],m,i,j,k=0,t;
cout<<"Enter the order\n";
cin>>m>>n;
cout<<"Enter the elements\n";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cin>>a[i][j];
b[k++]=a[i][j];
}
}
for(i=0;i<(m*n);i++)
{
t=b[i];
j=i;
while(j>0 && b[j-1]>=t)
{
b[j]=b[j-1];
j--;
}
b[j]=t;
}
cout<<"\n";
cout<<"Before sorting\n";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
cout<<a[i][j]<<"\t";
cout<<"\n";
}
cout<<"After sorting\n";
k=0;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
a[i][j]=b[k++];
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
cout<<a[i][j]<<"\t";
cout<<"\n";
}
getch();
}
Sample Output
Enter the order
3 3
9
8
7
4
5
2
1
3
6
Before sorting
9 8 7
4 5 2
1 3 6
After sorting
1 2 3
4 5 6
7 8 9
0 comments:
Post a Comment