Sorting 2d Arrays using std::sort (column based)
arrays, c++, sorting
Solution
You may write your own iterator, something like:
#include <iterator>
template<typename Container>
class column_iterator : public std::iterator<std::random_access_iterator_tag,
typename std::decay<decltype(std::declval<Container>()[0][0])>::type>
{
typedef typename Container::iterator iterator;
typedef typename std::decay<decltype(std::declval<Container>()[0][0])>::type type;
public:
column_iterator(iterator it, int n) : it(it), n(n) {}
column_iterator& operator++() {++it; return *this;}
column_iterator& operator++(int) { auto res(*this); ++*this; return res;}
column_iterator& operator +=(std::ptrdiff_t offset) { it += offset; return *this;}
column_iterator operator +(std::ptrdiff_t offset) const { auto res(*this); res += offset; return res;}
column_iterator& operator--() {--it; return *this;}
column_iterator& operator--(int) { auto res(*this); --*this; return res;}
column_iterator& operator -=(std::ptrdiff_t offset) { it -= offset; return *this;}
column_iterator operator -(std::ptrdiff_t offset) const { auto res(*this); res -= offset; return res;}
type& operator*() { return (*it)[n];}
type* operator->() { return &(it)[n];}
bool operator == (const column_iterator& rhs) const { return it == rhs.it && n == rhs.n; }
bool operator != (const column_iterator& rhs) const { return !(*this == rhs); }
bool operator < (const column_iterator& rhs) const { return it < rhs.it; }
std::ptrdiff_t operator -(const column_iterator& rhs) const { return it - rhs.it; }
private:
iterator it;
int n;
};
template<typename Container>
column_iterator<Container> begin(Container& cont, int n)
{
return column_iterator<Container>(cont.begin(), n);
}
template<typename Container>
column_iterator<Container> end(Container& cont, int n)
{
return column_iterator<Container>(cont.end(), n);
}
Now, let's test it:
#include <algorithm>
#include <array>
#include <iostream>
#include <vector>
#include <cassert>
void display(const std::vector<std::array<int, 5>>& v)
{
for (auto rows : v) {
for (auto elem : rows) {
std::cout << elem << " ";
}
std::cout << std::endl;
}
}
int main() {
std::vector<std::array<int, 5>> input {
{{ 13, 27, 4 , 1 , 11 }},
{{ 11, 19, 2 , 37, 1 }},
{{ 32, 64, 11, 22, 41 }},
{{ 71, 13, 27, -8, -2 }},
{{ 0 , -9, 11, 99, 13 }} };
for (int i = 0; i != 5; ++i) {
std::sort(begin(input, i), end(input, i));
}
display(input);
const std::vector<std::array<int, 5>> output {
{{ 0 , -9, 2 , -8, -2 }},
{{ 11, 13, 4 , 1 , 1 }},
{{ 13, 19, 11, 22, 11 }},
{{ 32, 27, 11, 37, 13 }},
{{ 71, 64, 27, 99, 41 }} };
assert(input == output);
return 0;
}
Problem
I'm running a test showing the benefits of sorting a 2d array, by columns, by pulling the data off into an individual array and sorting that array, then copying it back to the column. I'm wanting to run std::sort as a the sorting algorithm for every run. I'm trying to figure out how to run the loop in place first, before moving into the copying on and off the 2D array. An example of the input / output would be this. ``` #include <iostream> #include <algorithm> int main() { int input[][5] = { { 13, 27, 4 , 1 , 11 }, { 11, 19, 2 , 37, 1 }, { 32, 64, 11, 22, 41 }, { 71, 13, 27, -8, -2 }, { 0 , -9, 11, 99, 13 } }; // std::sort something here. int output[][5] = { { 0 , -9, 2 , -8, -2 }, { 11, 13, 4 , 1 , 1 }, { 13, 19, 11, 22, 11 }, { 32, 27, 11, 37, 13 }, { 71, 64, 27, 99, 41 } }; return 0; } ``` Thanks for the help.