Boost logo

Boost Users :

Subject: Re: [Boost-users] boost::sort::spreadsort::integer_sort
From: Steven Ross (spreadsort_at_[hidden])
Date: 2015-10-01 06:16:39


On Wed, Sep 30, 2015 at 8:04 PM <v9v_at_[hidden]> wrote:

> Hi!
>
> I'm a C++ beginner and any help would be very much appreciated.
> I would like to convert this "std::sort" into
> "boost::sort::spreadsort::integer_sort".
>

I recommend looking at the examples in the documentation:
http://www.boost.org/doc/libs/develop/libs/sort/doc/html/sort/sort_hpp/integer_sort.html#sort.sort_hpp.integer_sort.integersort_examples

>
> I know that I have to write "#include
> <boost/sort/spreadsort/integer_sort.hpp>" in
> my header instead of "#include <algorithm>" but after that, this is too
> difficult...
>
> Thanks!
>
> -------------------------------------------------------------------
> #include <iostream>
> #include <algorithm>
>
#include <boost/sort/spreadsort/spreadsort.hpp> // combined header
using namespace boost::sort::spreadsort;

using namespace std;
>

struct rightshift {
  rightshift(const int *const indexed_array) : nb1(indexed_array) {}
  inline int operator()(int x, unsigned offset) { return nb1[x] >> offset; }
  const int *const nb1;
};

>
> int main() {
>
> int nb1[4] = {15, 5, 0, 20};
> int nb2[4] = {99,102, 8, 2};
> int indices[4];
>
// integer_sort just calls std::sort when there are less than 1000 inputs.
// I tested with this code:
constexpr int length = 10000;
    int nb1[length];
    int nb2[length];
    int indices[length];
    for (int i = 0; i < length; ++i) {
      nb1[length - i - 1] = 2 * i;
      nb2[i] = i;
      indices[i] = i;
    }

>
> for (short i=0; i<4; i++) indices[i]=i;
>
> sort(begin(indices), end(indices), [&](int i1, int i2) { return
> nb1[i1] < nb1[i2]; } );
>
 integer_sort(begin(indices), end(indices), rightshift(nb1), [&](int i1,
int i2) { return nb1[i1] < nb1[i2]; });

>
> for (short i=0; i<4; i++) cout << nb1[indices[i]] << ' '; cout
> <<endl; // 0 5 15 20
> for (short i=0; i<4; i++) cout << nb2[indices[i]] << ' '; cout
> <<endl; // 8 102 99 2
>
> return 0;
> }
> ---------------------------------------------------------------------
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net