【问题标题】:Radix sort in singly-linked list C#单链表C#中的基数排序
【发布时间】:2023-03-08 13:35:01
【问题描述】:

我正在尝试在链接列表类中进行基数排序。我找到了数组的基数排序算法,并试图将其更改为与我的链表一起使用。但是,我有点挣扎。我试图更改的代码取自http://www.w3resource.com/csharp-exercises/searching-and-sorting-algorithm/searching-and-sorting-algorithm-exercise-10.php 我用数组测试了代码并且它有效。有人知道如何使基数排序在链表中工作吗?

//抽象类

abstract class DataList
{
    protected int length;
    public int Length { get { return length; } }
    public abstract double Head();
    public abstract double Next();
    public abstract void Swap(int a, int b);
    public void Print(int n)
    {
        Console.Write("{0} ", Head());
        for (int i = 1; i < n; i++)
            Console.Write("{0} ", Next());
        Console.WriteLine();
    }
}

//链表类

class LinkedList : DataList
{
    class MyLinkedListNode
    {
        public MyLinkedListNode nextNode { get; set; }
        public int data { get; set; }
        public MyLinkedListNode(int data)
        {
            this.data = data;
        }
        public MyLinkedListNode()
        {
            this.data = 0;
        }
    }
    MyLinkedListNode headNode;
    MyLinkedListNode prevNode;
    MyLinkedListNode currentNode;
    public LinkedList(int n, int min, int max)
    {
        length = n;
        Random rand = new Random();
        headNode = new MyLinkedListNode(rand.Next(min, max));
        currentNode = headNode;
        for (int i = 1; i < length; i++)
        {
            prevNode = currentNode;
            currentNode.nextNode = new MyLinkedListNode(rand.Next(min, max));
            currentNode = currentNode.nextNode;
        }
        currentNode.nextNode = null;
    }
    public LinkedList()
    {
        headNode = new MyLinkedListNode();
        currentNode = headNode;
    }
    public override double Head()
    {
        currentNode = headNode;
        prevNode = null;
        return currentNode.data;
    }
    public override double Next()
    {
        prevNode = currentNode;
        currentNode = currentNode.nextNode;
        return currentNode.data;
    }
    public override void Swap(int a, int b)
    {
        prevNode.data = a;
        currentNode.data = b;
    }

//我的基数排序

    public void radixSort()
    {
        int j = 0;
        LinkedList tmp = new LinkedList();
        for (int shift = 31; shift > -1; --shift)
        {
            //I try to go trough old list
            MyLinkedListNode current = headNode;
            while (current != null)
            {
                bool move = (current.data << shift) >= 0;
//I found this expression somewhere and I'm trying to use it to form a new Linked list (tmp)
                if (shift == 0 ? !move : move)
                    ;
                else
                {
                    if (tmp.headNode == null)
                        tmp.headNode = currentNode;
                    else
                    {
                        tmp.currentNode.nextNode = current;
//infinite loop happens on the commented line
                        //tmp.currentNode = tmp.currentNode.nextNode; 
                        j++;
                    }
                current = current.nextNode;
                }
            }
    }
}

【问题讨论】:

    标签: c# algorithm sorting linked-list radix-sort


    【解决方案1】:

    按照 C# 基数排序示例,您需要一个包含十个列表的数组。将节点从原始列表移动到十个列表中,将具有最低有效数字 == '0' 的节点附加到 array_of_lists[0] 中,将 '1' 附加到 array_of_list[1] 中,依此类推。清空原始列表后,将列表数组连接回原始列表并重复下一个最低有效位。重复此过程,直到处理完所有数字。

    您可以使用更大的基数,例如以 16 为基数,您可以使用 16 个列表的数组。然后,您可以使用 shift 和 and 选择每个“数字”。

    【讨论】:

      猜你喜欢
      • 2017-07-22
      • 2022-09-29
      • 2020-12-11
      • 2020-06-13
      • 2021-08-17
      • 1970-01-01
      • 2013-10-01
      • 2023-04-04
      • 1970-01-01
      相关资源
      最近更新 更多