sorting 2 dimensional dynamic array in ascending order

Hello!
I got a 2d dynamic array to sort.
If i have for an example ar array like:
321
231
321
i want to sort it to:
123
123
123
How should I tweak my code to make it work? I can't find a similar example anywhere else, so I help someone can help!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void sort(int **A,int n){
    int row=0;
do{
for(int i=0;i<n-1;i++)
        for(int j=1;j<n;j++)
        if(A[row][i]>A[row][j])
            swap(&A[row][i],&A[row][j]);
row++;
}while(row!=n+1);
}

void swap(int *num1, int *num2){
int temp = *num1;
*num1 = *num2;
*num2=temp;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

using matrix = vector< vector<int> >;

void print( const char *title, const matrix &A )
{
   cout << title << '\n';
   for ( int i = 0; i < A.size(); i++ )
   {
      for ( int j = 0; j < A[i].size(); j++ ) cout << A[i][j] << ' ';
      cout << '\n';
   }
}

int main()
{
   matrix A = { { 3, 2, 1 }, { 2, 3, 1 }, { 3, 2, 1 } };
   print( "Original array:", A );

   for ( auto &v : A ) sort( v.begin(), v.end() );
   print( "Array with rows sorted:", A );
}


Original array:
3 2 1 
2 3 1 
3 2 1 
Array with rows sorted:
1 2 3 
1 2 3 
1 2 3
Topic archived. No new replies allowed.