链表排序

using System;

namespace ConsoleApplication1
{
 

    class node
    {
        public int value;
        public node next;
        public node previous;
        public node maxNode(node x,node y)
        {
            if(y.value>=x.value)
                return y;
            else
                return x;
        }

    }
    class Class1
    {
        /// <summary>
        ///
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            //create a double linked list

            //each input of data should be ended with enter
            //if the input in finished,please press space,than enter
            string buffer;
            int nodeAccount=0;
            node superHead,superTail;
            node head=new node();
            superHead=head;
            superHead.previous=null;
            while((buffer=Console.ReadLine())!=" ")
            {
                head.value=Int32.Parse(buffer);
                node tail=new node();
                head.next=tail;
                tail.previous=head;
                head=tail;
                nodeAccount++;
            }
            superTail=head;
            superTail.next=null;
 
            //sort
            int outer,inner;
            node temp;
            node maxIndex;
           
            for(outer=nodeAccount;outer>0;outer--)
            {
                head=superHead;
                maxIndex=superHead;
                for(inner=0;inner<outer;inner++)
                {
                    maxIndex=head.maxNode(maxIndex,head);
                    head=head.next;
                }
                if(head!=null&&maxIndex.previous!=null)
                {
                    head=head.previous;
                   
                    //8 pointer exchanged
                    temp=maxIndex.next;
                    maxIndex.next=head.next;
                    head.next=temp;
               
                    temp=maxIndex.previous;
                    maxIndex.previous=head.previous;
                    head.previous=temp;

                    temp=maxIndex.next.previous;
                    maxIndex.next.previous=head.next.previous;
                    head.next.previous=temp;

                    temp=maxIndex.previous.next;
                    maxIndex.previous.next=head.previous.next;
                    head.previous.next=temp;               
                }

            }

            head=superHead;
            while(head!=superTail)//head is used as temporary variable
            {
                Console.WriteLine(head.value);
                head=head.next;

            }
                
        }
    }
}
/*
用单链表,只交换节点中的数据更方便的
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Python中,可以使用多种排序算法对链表进行排序。常见的适合链表排序的算法包括冒泡排序、选择排序、插入排序、归并排序、快速排序、计数排序、桶排序和基数排序。这些算法都可以应用于链表排序,并根据具体情况选择最合适的算法。不过,希尔排序链表排序中并不适合使用,而堆排序虽然可以用于链表排序,但不被建议使用。 具体实现链表排序的方法可以根据不同的排序算法进行选择。下面以归并排序为例,介绍一种Python实现链表排序的方法: 1. 定义一个函数mergeSort,该函数用于实现归并排序。 2. 在mergeSort函数中,先判断链表是否为空或只有一个节点,如果是,则返回链表。 3. 如果链表节点个数大于等于2,则将链表拆分成两个子链表,可以通过快慢指针的方法找到链表的中间节点,并将链表分成两部分。 4. 对两个子链表分别递归调用mergeSort函数,将它们分别排序。 5. 定义一个merge函数,用于合并两个已排序链表。 6. 在merge函数中,创建一个新的链表作为结果链表的头节点。 7. 依次比较两个链表的节点值,并将较小值的节点添加到结果链表中,直到其中一个链表为空。 8. 将另一个链表剩余的节点添加到结果链表的末尾。 9. 返回结果链表作为mergeSort函数的结果。 通过以上步骤,就可以实现对链表进行归并排序的功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值