Tuesday 3 October 2017

Sorting in 2d Array Using template functions in C++

#include<bits/stdc++.h>//TO INCLUDE ALL HEADER FILES
using namespace std;
main()
{
int n,m,i,j,k,temp;
cout<<"Enter Size Of 2d Array Row and Column:";
cin>>n>>m;
int a[n][m];
cout<<"Enter Element In 2d Array:";
for(i=0;i<n;i++)
for(j=0;j<m;j++)
cin>>a[i][j];

for(k=0;k<n;k++)
{

for(i=0;i<m;i++)
{

for(j=0;j<m-1;j++)
{

if(a[k][j]>a[k][j+1])
{
// temp=a[k][j];
//// a[k][j]=a[k][j+1];
//a[k][j+1]=temp;

swap(a[k][j],a[k][j+1]);
}

}
}
}


for(int i=0;i<n;i++)
{
cout<<"\n";
for(j=0;j<m;j++)
cout<<a[i][j]<<" ";

}

}

No comments:

Post a Comment

Amazon1Ads