首页 文章

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

提问于
浏览
1

我正在尝试编写一个快速算法来对大量整数的向量进行排序,例如:

159 14 5 97 6 54

到目前为止,我的程序将矢量分解为MSD的小桶,如:

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

现在我需要使用递归基数排序以最重要的数字顺序对存储桶进行排序:

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

这是我在网上找到的递归基数代码:

// 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);
  }
}

我不知道如何在我的代码中实现这一点,我不确定上面代码中的bucket []做了什么 . 任何人都可以解释我应该做出哪些改变?这是我正在使用的多线程代码,由于我没有使用递归,因此表现不佳 .

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());
  }
}

1 回答

  • 2

    我不知道如何将这个位实现到我的代码中,我不确定上面代码中的bucket []做了什么 . 任何人都可以解释我应该做出哪些改变?

    说实话,不需要桶[] . 这个想法是保持桶的索引在这里开始,但由于后来的桶以相同的顺序逐个处理,所以可以使用一些额外的变量而不是这个数组 .

    正如我所说,你应该将数字转换为字符串并对字符串进行排序 . 通过这种方式,您可以检查每个存储区中的1个字符,而不是执行所有create-string-> compare-> destroy-string操作 . 最后,您必须将字符串转换回数字 .

    你问的代码部分可能如下所示:

    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);
        }
    }
    

    要对字符串向量进行排序,您可以执行以下操作:

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

相关问题