LinkedList中的C#基数排序实现

问题描述 投票:0回答:1

我有一个为链表类创建基数排序算法的任务,我有一个对象“ Info”,它具有int Year和double Price,我需要使用Year来使用基数排序对链表进行排序。]

    class Info
    {
        public int Year { get; set; }
        public double Price { get; set; }
        public Info() { }
        public Info(int y, double p)
        {
            Year = y;
            Price = p;
        }
    }
    class Node
    {
        public Info Data { get; set; }
        public Node Next { get; set; }
        public Node(Info data, Node adress)
        {
            Data = data;
            Next = adress;
        }
    }
    class LinkedList
    {
        private Node First;
        private Node Last;
        private Node Current;
        public LinkedList()
        {
            First = null;
            Last = null;
            Current = null;
        }
     }

而且我从site中取了整数的基数排序算法。问题是,我不知道如何修改它以便与链接的类一起使用。

        static void Sort(int[] arr)
        {
            int temp = 0;
            int i, j;
            int[] tmp = new int[arr.Length];
            for (int shift = 31; shift > -1; --shift)
            {
                j = 0;
                for (i = 0; i < arr.Length; ++i)
                {
                    bool move = (arr[i] << shift) >= 0;
                    if (shift == 0 ? !move : move)
                        arr[i - j] = arr[i];
                    else
                        tmp[j++] = arr[i];
                }
                Array.Copy(tmp, 0, arr, arr.Length - j, j);
            }
        }

如何使其与链接的类一起工作?

我的任务是为链表类创建基数排序算法,我有一个对象“ Info”,它具有int Year和double Price,我需要使用Year来使用基数排序对链表进行排序。类...

c# singly-linked-list radix-sort
1个回答
0
投票

基于该代码,arr和tmp需要链接列表。这种方法的一个问题是,移动节点需要跟踪先前的节点才能移动节点。虚拟头节点可用于提供第一个数据节点之前的节点,或者在将节点移至列表开头时进行特殊情况处理。一种替代方法是使用两个指向临时列表节点的指针(引用),一个指向位== 0,另一个指向位== 1,然后将两个临时列表连接成一个列表。请注意,此方法需要32次通过。如果基数排序是基于字节而不是位,则可以将其减少到4次,但是需要256个指向256个列表的节点的指针。

© www.soinside.com 2019 - 2024. All rights reserved.