0

If I have a matrix like this:

 4 5 3
 6 8 7
 9 5 4
 2 1 3

and I want only to sort only the first two rows such that I get:

 3 4 5
 6 7 8
 9 5 4
 2 1 3

How can I achieve that using C++14?

Sudesh Chaudhary
  • 395
  • 6
  • 13
v_head
  • 759
  • 4
  • 13

2 Answers2

2

In your expected output, what you sort are the rows, so your title is not accurate.

Taking the sample output you present:

Live demo

int mat[][3] = { {4, 5, 3},
                 {6, 8, 7},
                 {9, 5, 4},
                 {2, 1, 3} }; 

Given the C-style 2D array, to sort the first 2 rows:

#include <algorithm>
//...                
std::sort(std::begin(mat[0]), std::end(mat[0]));
std::sort(std::begin(mat[1]), std::end(mat[1]));

To sort the whole array, you would use a cycle:

for(size_t i = 0; i < sizeof(mat) / sizeof(mat[0]); i++) //deduce the number of rows
    std::sort(std::begin(mat[i]), std::end(mat[i]));

Output:

3 4 5
6 7 8
9 5 4
2 1 3

If you want to use a C++ container like, let's say, a vector of vectors, as it would be recommended, or for a fixed size array a std::array:

Sample to sort the whole 2D vector (The same method for std::array)

std::vector<std::vector<int>> mat = {
    {4, 5, 3},
    {6, 8, 7},
    {9, 5, 4},
    {2, 1, 3}};

for(size_t i = 0; i < mat.size(); i++)
    std::sort(std::begin(mat[i]), std::end(mat[i]));

As you can see it's a more friendly approach give that C++ containers have a member which stores its own size.

Output:

3 4 5 
6 7 8 
4 5 9 
1 2 3 
anastaciu
  • 23,467
  • 7
  • 28
  • 53
  • if I am using a vector, and I want to sort all columns, I do: `sort(vect[0].begin(), vect[n-1].end());` ? So instead if I want to get: `3 4 5 6 7 8 4 5 9 1 2 3` read them as matrix – v_head May 25 '20 at 09:39
  • You mean a vector of vectors like `std::vector>` ? – anastaciu May 25 '20 at 09:48
  • `std::sort(std::begin(mat[0]), std::end(mat[1]));` is pedantically UB, as the pointers don't belong to same array. – Jarod42 May 25 '20 at 10:09
  • @Jarod42, do C-style arrays use iterators? – anastaciu May 25 '20 at 10:11
  • @Jarod42, so is it valid like it is? Given that 2D C-style arrays are stored in line? – anastaciu May 25 '20 at 10:22
  • No, you cannot treat 2D-C-array as 1D-C-array. `constexpr` is a good way to detect UB, and as you can see [here](https://godbolt.org/z/Vw-Hf8) that arithmetic is wrong. – Jarod42 May 25 '20 at 10:33
  • @user13584915, if you have a 2D vector which makes sense for a matrix, you can't do it like that because of the intrinsic nature of iterators, I added a sorting method for a 2D vector to my answer. – anastaciu May 25 '20 at 10:39
  • @Jarod42, I fixed it, not to abuse your good will, but what if I use it like `int (*mat)[3]`? Would it still be UB? [sample](https://wandbox.org/permlink/7o0mLpslbRvTUbsC), and what about in C? Sorry to take up your time. – anastaciu May 25 '20 at 11:22
  • @Jarod42, I [asked a question](https://stackoverflow.com/q/62001869/6865932) regarding this, at least you would earn something by answering it. – anastaciu May 25 '20 at 11:56
1

C++ STL provides a function sort that sorts a vector or array. The average of a sort complexity is N*log2 (N)

Syntax:

sort(first, last);

Here, first – is the index (pointer) of the first element in the range to be sorted. last – is the index (pointer) of the last element in the range to be sorted.

Example:

For sorting the first row of the matrix:

sort(m[0].begin(),m[0].end());

you can use a loop to sort some n rows like this:

    for(int i=0;i<n;i++)
    {
     sort(m[i].begin(),m[i].end());
    }

by default sort() function sort the array in ascending order. If you want to sort in descending order then for a vector v ,you can do it like this:

sort(v.begin(),v.end(),greater<int>());

if you are using an array (assume arr where the size of arr is n)then you can use the sort function like this:

sort(arr,arr+n)
Sudesh Chaudhary
  • 395
  • 6
  • 13