单链表的排序

本文详细介绍了单链表的四种排序方法:插入排序、选择排序、冒泡排序和递归排序,分别从原理到实现进行了阐述。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、单链表的插入排序

ListNode *insertionSortList(ListNode *head) 
{
	if(head==NULL||head->next==NULL)
		return head;

	ListNode*p=NULL;
	ListNode*q=NULL;
	ListNode*one=NULL;
	ListNode*first=head;
	head=head->next;
	first->next=NULL;
	while(head)
	{
		for (p=first,one=head;p!=NULL&&one!=NULL&&p->val<one->val;q=p,p=p->next);
		head=head->next;
		if (p==first)
		{
			first=one;
		}
		else
		{
			q->next=one;
		}
		one->next=p;
	}
	return first;
}

二、单链表的选择排序

ListNode *ChooseSortList(ListNode *head) 
{
	if(head==NULL||head->next==NULL)
		return head;
	ListNode*first=NULL;
	ListNode*p=NULL;
	ListNode*q=NULL;
	ListNode*min=NULL;
	ListNode*trail=NULL;
	while(head)
	{
		for(q=head,min=head;q->next!=NULL;q=q->next)
		{
			if (q->next->val<min->val)
			{
				p=q;
				min=q->next;
			}
		}
		if(first==NULL)
		{
			first=min;
			trail=min;
		}
		else
		{
			trail->next=min;
			trail=min;
		}
		if (min==head)
		{
			head=head->next;
		}
		else
		{
			p->next=min->next;
		}
	}
	if (trail)
	{
		trail->next=NULL;
	}
	return first;
}

三、单链表的冒泡排序


ListNode*BubbleSort(ListNode*head)
{
if(head==NULL||head->next==NULL)
return head;
ListNode*trail=NULL;
bool flag=true;
while(head!=trail&&flag)
{
ListNode*temp=head;
ListNode*last=head;

flag=false;
for(;temp->next!=trail;last=temp,temp=temp->next)
{

if(temp->val>temp->next->val)
{
if (temp==head)
{
ListNode*pcur=temp->next;
temp->next=pcur->next;
pcur->next=temp;
temp=pcur;
head=pcur;
}
else
{
ListNode*pcur=temp->next;
temp->next=pcur->next;
pcur->next=temp;
last->next=pcur;
temp=pcur;
}
flag=true;
}
}
trail=temp;
}
return head;
}


四、单链表的递归排序

1)普通的归并排序,即数组
//普通的归并
void Marge(int s[],int t[],int begin,int end,int mid)
{
	int k,l,m;
	for(k=begin,l=mid+1;begin<=mid&&l<=end;k++)
	{
		if(s[begin]<s[l])
			t[k]=s[begin++];
		else
			t[k]=s[l++];
	}
	if (begin<=mid)
	{
		for(m=0;m<=mid-begin;m++)
			t[k+m]=s[begin+m];
	}
	if (l<=end)
	{
		for(m=0;m<=mid-begin;m++)
			t[k+m]=s[begin+m];
	}
}
void Msort(int s[],int t[],int begin,int end,int n)
{
	int *t1=(int*)malloc(sizeof(int)*n);
	if (begin==end)
	{
		t[begin]=s[begin];
	}
	else
	{
		int mid=(begin+end)/2;
		Msort(s,t1,begin,mid,n);
		Msort(s,t1,mid+1,end,n);
		Marge(t1,t,begin,end,mid);
	}
}
void Margetsort(int a[],int n)
{
	Msort(a,a,0,n-1,n);
}

2)链表的递归排序
    ListNode* findMiddle(ListNode* head)
    {
        ListNode*p1=head->next;
        ListNode*p2=head;
        while(p1!=NULL&&p1->next!=NULL)
        {
            p1=p1->next->next;
            p2=p2->next;
        }
        return p2;
    }
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) 
    {
        ListNode*phead=NULL;
        if(l1==NULL)
            return l2;
        if (l2==NULL)
            return l1;
        if(l1->val>l2->val)
        {
            phead=l2;
            l2=l2->next;
        }
        else
        {
            phead=l1;
            l1=l1->next;
        }
        ListNode*pcur=phead;
        while(l1!=NULL&&l2!=NULL)
        {
            if(l1->val<l2->val)
            {
                pcur->next=l1;
                l1=l1->next;
            }
            else
            {
                pcur->next=l2;
                l2=l2->next;
            }
            pcur=pcur->next;
        }
        if(l1!=NULL)
        {
            pcur->next=l1;
        }
        if (l2!=NULL)
        {
            pcur->next=l2;
        }
        return phead;
    }
    ListNode *sortList(ListNode *head) 
    {
        if (head==NULL||head->next==NULL)
        {
            return head;
        }
        ListNode*mid=findMiddle(head);
        ListNode*right=sortList(mid->next);
        mid->next=NULL;
        ListNode*left=sortList(head);
        return mergeTwoLists(left,right);
    }






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值