简体   繁体   English

使用C ++进行基数排序

[英]Radix Sort using C++

Suppose I have bunch of numbers. 假设我有一堆数字。 I have to first put the least significant digit into the corresponding bucket. 我必须首先将最低有效位放入相应的存储桶中。 Ex: 530 , I have to first put into the bucket 0. For number 61, I have to put into bucket 1. 例如:530,我必须首先将存储桶0放入。对于数字61,我必须将存储桶1放入。

I planned to use a multidimensional array to do this. 我计划使用多维数组来执行此操作。 So I create a 2-dimenional array, which nrows is 10 ( for 0~ 9) and ncolumns is 999999 ( because I don't know how large will the list be): 因此,我创建了一个二维数组,其行数为10(对于0〜9),ncolumns为999999(因为我不知道列表的大小):

    int nrows = 10;
    int ncolumns = 999999;

    int **array_for_bucket = (int **)malloc(nrows * sizeof(int *));
         for(i = 0; i < nrows; i++)
             array_for_bucket[i] = (int *)malloc(ncolumns * sizeof(int));

    left = (a->value)%10;
    array_for_bucket[left][?? ] = a->value;

Then I created one node call a. 然后,我创建了一个节点调用a。 In this node a, there is a value 50. To find out which bucket I want to put it in, I calculate "left" and I got 0. So I want to put this a-> value into bucket 0. But now I am stuck. 在此节点a中,有一个值50。要找出我要放入哪个存储桶,我计算“ left”,然后得到0。所以我想将此a->值放入存储桶0。但是现在我卡住了。 How do I put this value into the bucket? 我该如何将这个值放入存储桶中? I have to use a pointer array to do this. 我必须使用指针数组来执行此操作。

I thought for a long time but still couldn't find a good way to do it. 我想了很长时间,但仍然找不到找到它的好方法。 So please share some ideas with me. 因此,请与我分享一些想法。 thank you! 谢谢!

There is a much easier way of doing this, and instead of radix*nkeys space you only need an nkeys -sized buffer. 有一种更简单的方法,而不是radix*nkeys空间,您只需要一个nkeys大小的缓冲区。

Allocate a second buffer that can fit nkeys keys. 分配可以容纳nkeys键的第二个缓冲区。 Now do a first pass through your data and simply count how many keys end up in each bucket. 现在,先对数据进行一次遍历,然后简单地计算每个存储桶中最终有多少个密钥。 You now can create a radix -sized array of pointers where each pointer is to the start of that bucket in the output buffer. 现在,您可以创建一个radix大小的指针数组,其中每个指针都指向输出缓冲区中该存储桶的开头。 Finally, the second pass though the data moves the keys. 最后,第二遍通过数据移动密钥。 Every time you move a key, increment that bucket pointer. 每次移动键时,增加该存储桶指针。

Here's some C code to make into C++: 这是一些要用C ++编写的C代码:

void radix_sort(int *keys, int nkeys)
{
    int *shadow = malloc(nkeys * sizeof(*keys));

    int bucket_count[10];
    int *bucket_ptrs[10];
    int i;

    for (i = 0; i < 10; i++)
        bucket_count[i] = 0;

    for (i = 0; i < nkeys; i++)
        bucket_count[keys[i] % 10]++;

    bucket_ptrs[0] = shadow;
    for (i = 1; i < 10; i++)
        bucket_ptrs[i] = bucket_ptrs[i-1] + bucket_count[i-1];

    for (i = 0; i < nkeys; i++)
        *(bucket_ptrs[keys[i] % 10]++) = keys[i];

    //shadow now has the sorted keys

    free(shadow);
}

But I may have misunderstood the question. 但是我可能误解了这个问题。 If you are doing something a little different than radix sort, pleas add some details. 如果您要执行的操作与基数排序有所不同,请添加一些详细信息。

如果要存储指针,请查看Boost Pointer容器库。

C++ isn't my forte but this code from wikipedia-Raidx Sort is very comprehensive and probably is more C++-ish than what you've implemented so far. C ++不是我的强项,但Wikipedia-Raidx Sort的这段代码非常全面,可能比到目前为止实现的C ++ 风格更多。 Hope it helps 希望能帮助到你

This is C++, we don't use malloc anymore. 这是C ++,我们不再使用malloc We use containers. 我们使用容器。 A two-dimensional array is a vector of vectors. 二维数组是向量的向量。

vector<vector<int> > bucket(10);
left = (a->value)%10;
bucket[left].push_back(a->value);

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

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