一、单链表的插入排序
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;
}
{
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);
}