繁体   English   中英

为什么My Base 10 LSD Radix-Sort比我的位移基数排序更快?

[英]Why is My Base 10 LSD Radix-Sort Faster Than My Bit-Shifted Radix Sort?

所以我正在为一个学校项目试验不同种类的Radix排序,我们必须尽快排序500,000个随机整数(我自己生成,每个整数的界限在0到MaxValue之间)。 我首先制作了一个10 LSD(最低有效数字)基数排序,平均大约110到115毫秒,用于排序500,000个随机整数。 这是它的代码:

public static int[] RadixSort(int[] RandomNumbers)
        {
            List<int>[] Buckets = new List<int>[10];

            int singleDigit = 0;
            int[] temp;
            int[] mult = new int[10] {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000};

            for (int z = 0; z < 10; z++)
            {
                Buckets[0] = new List<int>();
                Buckets[1] = new List<int>();
                Buckets[2] = new List<int>();
                Buckets[3] = new List<int>();
                Buckets[4] = new List<int>();
                Buckets[5] = new List<int>();
                Buckets[6] = new List<int>();
                Buckets[7] = new List<int>();
                Buckets[8] = new List<int>();
                Buckets[9] = new List<int>();

                if (z == 0)
                {
                    temp = (int[])RandomNumbers.Clone();
                }
                else
                {
                    temp = (int[])RandomNumbers.Clone();

                    for (int i = 0; i < SIZE; i++)
                    {
                        temp[i] /= (mult[z]);
                    }
                }

                for (int i = 0; i < SIZE; i++)
                {
                    singleDigit = temp[i] % 10;

                    Buckets[singleDigit].Add(RandomNumbers[i]);
                }

                List<int> NewList = new List<int>(SIZE);

                for (int i = 0; i < 10; i++)
                {
                    NewList.AddRange(Buckets[i]);
                }

                int[] NewArray = NewList.ToArray();

                RandomNumbers = NewArray;
            }

            return RandomNumbers;
        }

但是,我听说在位移基数排序中使用二进制更快。 因此,我创建了一个基于掩码的位移基数排序,整体看起来不那么混乱,并且在其中运行的操作较少,但其平均排序速度大约为250毫秒。 这是它的代码:

public static int[] BitShiftRadixSort(int[] RandomNumbers)
        {
            List<int>[] Buckets = new List<int>[2];
            int binary;
            int mask;

            for (int shift = 0; shift < 32; shift++)
            {
                Buckets[0] = new List<int>(SIZE);
                Buckets[1] = new List<int>(SIZE);

                mask = 1 << shift;

                for (int i = 0; i < SIZE; i++)
                {
                    binary = RandomNumbers[i] & mask;

                    if (binary != 0)
                    {
                        Buckets[1].Add(RandomNumbers[i]);
                    }
                    else
                    {
                        Buckets[0].Add(RandomNumbers[i]);
                    }
                }

                List<int> NewList = new List<int>(SIZE);

                for (int i = 0; i < 2; i++)
                {
                    NewList.AddRange(Buckets[i]);
                }

                int[] NewArray = NewList.ToArray();

                RandomNumbers = NewArray;
            }

            return RandomNumbers;
        }

我期待比特移位比LSD Radix排序快,但事实并非如此。 C#中的数学运算是否经过大量优化? 我会感谢大家的投入!

使用Matt Timmermans建议使用base 256,对于500,000个32位无符号整数,此版本对于无符号整数大约需要10ms。 对于有符号整数,您需要补充符号位并将它们视为无符号整数(然后补充符号返回)。

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;

namespace RadixSort
{
    class Program
    {

        static void RadixSort(uint [] a, uint count)
        {
        uint [,] mIndex = new uint [4,256];     // count / index matrix
        uint [] b = new uint [count];           // allocate temp array
        uint i,j,m,n;
        uint u;
            for(i = 0; i < count; i++){         // generate histograms
                u = a[i];
                for(j = 0; j < 4; j++){
                    mIndex[j,(u & 0xff)]++;
                    u >>= 8;
                }       
            }
            for(j = 0; j < 4; j++){             // convert to indices
                m = 0;
                for(i = 0; i < 256; i++){
                    n = mIndex[j,i];
                    mIndex[j,i] = m;
                    m += n;
                }       
            }
            for(j = 0; j < 4; j++){             // radix sort
                for(i = 0; i < count; i++){     //  sort by current lsb
                    u = a[i];
                    m = (u>>((int)(j<<3)))&0xff;
                    b[mIndex[j,m]++] = u;
                }
                uint [] t = a;                  //  swap references
                a = b;
                b = t;
            }
        }

        static void Main(string[] args)
        {
            const int SIZE = 500000;
            uint [] a = new uint[SIZE];
            uint u;
            Random r = new Random(1);
            Stopwatch sw = new Stopwatch();
            for (uint i = 0; i < a.Length; i++)
            {
                u = (uint)r.Next(1 << 16);
                u = (u << 16) | (uint)r.Next(1 << 16);
                a[i] = u;
            }
            sw.Start();
            RadixSort(a, (uint)a.Length);
            sw.Stop();
            for (uint i = 1; i < a.Length; i++)
            {
                if(a[i] < a[i-1])
                {
                    Console.WriteLine("failed");
                    break;
                }
            }
            Console.WriteLine("milliseconds: {0:D}\n",sw.ElapsedMilliseconds);
        }
    }
}

暂无
暂无

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

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