In 2D array sorting as i seen they just copied it to single dimension array and sorted it. But is there any other way to sort 2 dimensional array without using 1D array.
// code
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int rows, columns;
int k = 0, temp;
int rowColumn;
//getting rows and columns number
cout<<"Enter number of rows";
cin>>rows;
cout<<"Enter number of columns";
cin>>columns;
//declaring and intitalizing oneD and twoD array
rowColumn = rows * columns;
int arr[rows][columns];
int oneDArr[rowColumn];
//Fill 2D array by user
cout<<"Fill 2D array row wise"<<endl;
for(int i=0; i<rows; i++)
{
for(int j=0; j<columns; j++)
{
cin>>arr[i][j];
}
}
//Taking 2d array in 1d array
for(int i=0; i<rows; i++)
{
for(int j=0; j<columns; j++)
{
oneDArr[k] = arr[i][j];
k++;
}
}
//Bubble sort perform on 1d array
for(int j=1;j<rowColumn;j++)
{
for(int i=0; i<rowColumn; i++)
{
if(oneDArr[i]>oneDArr[i+1])
{
temp=oneDArr[i];
oneDArr[i] = oneDArr[i+1];
oneDArr[i+1]=temp;
}
}
}
//rearranging the oneD array to twoD array
k = 0;
for(int i=0; i<rows; i++)
{
for(int j=0; j<columns; j++)
{
arr[i][j] = oneDArr[k];
k++;
}
}
//Displaying sorted 2d Array
for(int i=0; i<rows; i++)
{
for(int j=0; j<columns; j++)
{
cout<<arr[i][j]<<"\t";
}
cout<<"\n";
}
}
Is there any other way to sort 2D array with efficiency.