有现成的 C# 基数排序实现吗?

发布于 2024-08-02 18:35:21 字数 1539 浏览 5 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你不是我要的菜∠ 2024-08-09 18:35:21

这是来自 wikibooks 的一本(这是基于最低有效数字的)

public void RadixSort(int[] a)
{  
    // our helper array 
    int[] t=new int[a.Length]; 

    // number of bits our group will be long 
    int r=4; // try to set this also to 2, 8 or 16 to see if it is 
             // quicker or not 

    // number of bits of a C# int 
    int b=32; 

    // counting and prefix arrays
    // (note dimensions 2^r which is the number of all possible values of a 
    // r-bit number) 
    int[] count=new int[1<<r]; 
    int[] pref=new int[1<<r]; 

    // number of groups 
    int groups=(int)Math.Ceiling((double)b/(double)r); 

    // the mask to identify groups 
    int mask = (1<<r)-1; 

    // the algorithm: 
    for (int c=0, shift=0; c<groups; c++, shift+=r)
    { 
        // reset count array 
        for (int j=0; j<count.Length; j++)
            count[j]=0;

        // counting elements of the c-th group 
        for (int i=0; i<a.Length; i++)
            count[(a[i]>>shift)&mask]++; 

        // calculating prefixes 
        pref[0]=0; 
        for (int i=1; i<count.Length; i++)
            pref[i]=pref[i-1]+count[i-1]; 

        // from a[] to t[] elements ordered by c-th group 
        for (int i=0; i<a.Length; i++)
            t[pref[(a[i]>>shift)&mask]++]=a[i]; 

        // a[]=t[] and start again until the last group 
        t.CopyTo(a,0); 
    } 
    // a is sorted 
}

here is one from wikibooks (This is Least Significant Digit based)

public void RadixSort(int[] a)
{  
    // our helper array 
    int[] t=new int[a.Length]; 

    // number of bits our group will be long 
    int r=4; // try to set this also to 2, 8 or 16 to see if it is 
             // quicker or not 

    // number of bits of a C# int 
    int b=32; 

    // counting and prefix arrays
    // (note dimensions 2^r which is the number of all possible values of a 
    // r-bit number) 
    int[] count=new int[1<<r]; 
    int[] pref=new int[1<<r]; 

    // number of groups 
    int groups=(int)Math.Ceiling((double)b/(double)r); 

    // the mask to identify groups 
    int mask = (1<<r)-1; 

    // the algorithm: 
    for (int c=0, shift=0; c<groups; c++, shift+=r)
    { 
        // reset count array 
        for (int j=0; j<count.Length; j++)
            count[j]=0;

        // counting elements of the c-th group 
        for (int i=0; i<a.Length; i++)
            count[(a[i]>>shift)&mask]++; 

        // calculating prefixes 
        pref[0]=0; 
        for (int i=1; i<count.Length; i++)
            pref[i]=pref[i-1]+count[i-1]; 

        // from a[] to t[] elements ordered by c-th group 
        for (int i=0; i<a.Length; i++)
            t[pref[(a[i]>>shift)&mask]++]=a[i]; 

        // a[]=t[] and start again until the last group 
        t.CopyTo(a,0); 
    } 
    // a is sorted 
}
计㈡愣 2024-08-09 18:35:21
    public static int[] radixSort(int[] ar)
    {
        int width = 0;
        foreach (int el in ar)
        {
            int numDigits = el.ToString().Length;
            if (numDigits > width)
                width = numDigits;
        }

        int md, n;

        Dictionary<int, LinkedList> queue = null;

        Action refreshQueue = () =>
       {
           queue = new Dictionary<int, LinkedList>();

           for (int i = 0; i <= 9; i++)
           {
               queue[i] = null;
           }
       };

        refreshQueue();

        for (int i = 1; i <= width; i++)
        {
            md = (int)Math.Pow(10, i); 
            n = md / 10; 

            foreach (int el in ar)
            {
                int ithPlace = (int)((el % md) / n);
                if (queue[ithPlace] == null)
                    queue[ithPlace] = new LinkedList(new LinkedListNode(el));
                else
                    queue[ithPlace].add(new LinkedListNode(el));
            }

            List<int> newArray = new List<int>();
            for (int k = 0; k <= 9; k++)
            {
                if (queue[k] != null)
                {
                    LinkedListNode head = queue[k].head;
                    while (head != null)
                    {
                        newArray.Add(head.value);
                        head = head.next;
                    }
                }
            }
            ar = newArray.ToArray();
            refreshQueue();
        }

        return ar;
    }
    public static int[] radixSort(int[] ar)
    {
        int width = 0;
        foreach (int el in ar)
        {
            int numDigits = el.ToString().Length;
            if (numDigits > width)
                width = numDigits;
        }

        int md, n;

        Dictionary<int, LinkedList> queue = null;

        Action refreshQueue = () =>
       {
           queue = new Dictionary<int, LinkedList>();

           for (int i = 0; i <= 9; i++)
           {
               queue[i] = null;
           }
       };

        refreshQueue();

        for (int i = 1; i <= width; i++)
        {
            md = (int)Math.Pow(10, i); 
            n = md / 10; 

            foreach (int el in ar)
            {
                int ithPlace = (int)((el % md) / n);
                if (queue[ithPlace] == null)
                    queue[ithPlace] = new LinkedList(new LinkedListNode(el));
                else
                    queue[ithPlace].add(new LinkedListNode(el));
            }

            List<int> newArray = new List<int>();
            for (int k = 0; k <= 9; k++)
            {
                if (queue[k] != null)
                {
                    LinkedListNode head = queue[k].head;
                    while (head != null)
                    {
                        newArray.Add(head.value);
                        head = head.next;
                    }
                }
            }
            ar = newArray.ToArray();
            refreshQueue();
        }

        return ar;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文