简体   繁体   English

在并行存储桶排序中使用递归基数排序

[英]Using recursive radix sort in parallel bucket sort

I'm trying to write a fast algorithm to sort a vector of a large amount of integers like: 我正在尝试编写一种快速算法来对大量整数的向量进行排序,例如:

159 14 5 97 6 54

so far, my program breaks the vector into small buckets by the MSD like: 到目前为止,我的程序已通过MSD将向量分成小块,例如:

bucket[1]:159 14
bucket[5]:5 54
bucket[6]:6
bucket[9]:97

and now I'm required to use recursive radix sort to sort the bucket in most significant digit order: 现在我需要使用递归基数排序以最高有效数字顺序对存储桶进行排序:

bucket[1]:14 159
bucket[5]:5 54
bucket[6]:6
bucket[9]:97

This is the recursive radix code I found online: 这是我在网上找到的递归基数代码:

// Sort 'size' number of integers starting at 'input' according to the 'digit'th digit
// For the parameter 'digit', 0 denotes the least significant digit and increases as significance does
void radixSort(int* input, int size, int digit){
  if (size == 0)
    return;

  int[10] buckets;    // assuming decimal numbers

  // Sort the array in place while keeping track of bucket starting indices.
  // If bucket[i] is meant to be empty (no numbers with i at the specified digit),
  // then let bucket[i+1] = bucket[i]

  for (int i = 0; i < 10; ++i)
  {
    radixSort(input + buckets[i], buckets[i+1] - buckets[i], digit+1);
  }
}

I don't know how to implement this bit into my code, I'm not sure about what bucket[] do in the code above. 我不知道如何在我的代码中实现这一点,我不确定上面代码中的bucket []是做什么的。 Can anyone explain what changes should I make? 谁能解释我应该做哪些更改? Here's the multithread code I'm using which is not performing well since I'm not using recursive. 这是我正在使用的多线程代码,由于我没有使用递归,所以效果不佳。

void sort(unsigned int numCores, std::vector<unsigned int> numbersToSort){
// ******************Stage 1****************
// Use multithread to seperate numbers into buckets using the most significant digits
  auto smallbuckets = std::vector<std::shared_ptr<std::vector<std::vector<unsigned int>>>>();
  std::mutex mutex;

  unsigned int workload = numbersToSort.size() / numCores;

  std::function<void(unsigned int, unsigned int, unsigned int)> put_small_buckets;
  put_small_buckets = [this, &smallbuckets, &mutex]
(unsigned int id, unsigned int start, unsigned int end) {

    auto buckets = std::make_shared<std::vector<std::vector<unsigned int>>>(std::vector<std::vector<unsigned int>>());
    for (int j = 0; j < 10; ++j) {
        buckets->push_back(std::vector<unsigned int>());
    }

    for (unsigned int i = start; i < end; ++i) {
        unsigned int a = numbersToSort[i];
        std::string tmp = std::to_string(a);
        char c = tmp.at(0);
        int ia = c - '0';
        (*buckets)[ia].push_back(numbersToSort[i]);
    }
    std::lock_guard<std::mutex> lock(mutex);
    smallbuckets.push_back(buckets);
  };

// create a container of threads
  std::vector<std::shared_ptr<std::thread>> containerOfThreads;

// create threads and add them to the container.
  for (unsigned int i = 0; i < numCores; ++i) {
    // start the thread.
    unsigned int start = workload * i;
    unsigned int end = workload * (i + 1);
    if(i == numCores - 1) end = this->numbersToSort.size() ;
    containerOfThreads.push_back(std::make_shared<std::thread>(put_small_buckets, i, start, end));
  }

// join all the threads back together.
  for (auto t : containerOfThreads) t->join();

  numbersToSort.clear();
// ******************Stage 2****************
// Put small multithreaded buckets back to the bucket of radix(10)

  auto bigbuckets = std::vector<std::shared_ptr<std::vector<unsigned int>>>();
  for (int j = 0; j < 10; ++j) {
    bigbuckets.push_back(std::make_shared<std::vector<unsigned int>>(std::vector<unsigned int>()));
  }

int current_index = 10;

std::function<void()> collect;
collect = [this, &smallbuckets, &current_index, &mutex, &collect, &bigbuckets] () {
    mutex.lock();
    int index = --current_index;
    mutex.unlock();
    if (index < 0) return;
    auto mybucket = bigbuckets[index];
    for (auto i = smallbuckets.begin(); i != smallbuckets.end(); ++i) {
        mybucket->insert(mybucket->end(), (*(*i))[index].begin(), (*(*i))[index].end());
    }
    collect();
  };

// create a container of threads
  containerOfThreads.clear();

// create threads and add them to the container.
  for (unsigned int i = 0; i < numCores; ++i) {
    containerOfThreads.push_back(std::make_shared<std::thread>(collect));
  }

// join all the threads back together.
  for (auto t : containerOfThreads) t->join();

// ******************Stage 3****************
// Sort big buckets

  for (int j = 0; j < 10; ++j) {
    bigbuckets.push_back(std::make_shared<std::vector<unsigned int>>(std::vector<unsigned int>()));
  }
  std::function<void(unsigned int, unsigned int)> sort_big_buckets;
  sort_big_buckets = [this, &bigbuckets, &mutex]
  (unsigned int start, unsigned int end) {
    unsigned int curr = start;
    while(curr < end){

        auto mybucket = bigbuckets[curr];
        std::sort(mybucket->begin(),mybucket->end(), [](const unsigned int& x, const unsigned int& y){
            std::string tmp1 = std::to_string(x);
            std::string tmp2 = std::to_string(y);
            return lexicographical_compare(tmp1.begin(), tmp1.end(), tmp2.begin(), tmp2.end());
            //return aLessB(x,y,0);
        } );
        ++curr;
    }
  };
// create a container of threads
  containerOfThreads.clear();

  workload = 10 / numCores;
// create threads and add them to the container.
  for (unsigned int i = 0; i < numCores; ++i) {
    // start the thread.
    unsigned int start = workload * i;
    unsigned int end = workload * (i + 1);
    if(i == numCores - 1) end = 10 ;
    containerOfThreads.push_back(std::make_shared<std::thread>(sort_big_buckets, start, end));
  }

// join all the threads back together.
  for (auto t : containerOfThreads) t->join();
// put all elements back to numbersToSort
  for (auto i = bigbuckets.begin(); i != bigbuckets.end(); ++i) {
    numbersToSort.insert(numbersToSort.end(), (*i)->begin(), (*i)->end());
  }
}

I don't know how to implement this bit into my code, I'm not sure about what bucket[] do in the code above. 我不知道如何在我的代码中实现这一点,我不确定上面代码中的bucket []是做什么的。 Can anyone explain what changes should I make? 谁能解释我应该做哪些更改?

To be honest, buckets[] is not needed. 老实说,不需要buckets []。 The idea is to keep indexes of bucket beginnings here, but since later buckets are processed in the same order one by one, it's possible to use a couple of additional variables instead of this array. 这个想法是在此处保留存储桶开头的索引,但是由于以后的存储桶以相同的顺序一个接一个地处理,因此可以使用几个其他变量来代替此数组。

As I've said, you should convert numbers to strings and sort strings. 就像我说过的,您应该将数字转换为字符串并对字符串进行排序。 This way you'll be able to check 1 character per bucketing, not doing all create-string->compare->destroy-string operations. 这样,您就可以每个存储桶检查1个字符,而无需执行所有create-string-> compare-> destroy-string操作。 In the end you'll have to convert strings back to numbers. 最后,您必须将字符串转换回数字。

The part of code you were asking can look like this: 您要求的部分代码如下所示:

void radixSort(std::vector<std::string>::iterator begin, std::vector<std::string>::iterator end, int digit){
    if (begin == end)
        return;

    // first skip short numbers
    e = begin;
    for (auto p = begin; p != end; ++p)
        if (p->size() <= digit)
        {
            if (p != e)
                std::swap(*p, *e);
            q++;
        }
    if (e == end)
        return;

    for (char d = '0'; d <= '9'; ++d)
    {
        auto s = e;
        for (auto p = e; p != end; ++p)
            if (p->at(digit) == d)
            {
                if (p != e)
                    std::swap(*p, *e);
                e++;
            }
        radixSort(s, e, digit+1);
    }
}

To sort string vector you can do something like this: 要对字符串向量进行排序,您可以执行以下操作:

radixSort(v.begin(), v.end(), 0);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM