struct ListNode {//l链表节点定义
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
链表的快速排序法:
ListNode * Partion(ListNode *beg, ListNode *end)
{
int key = beg->val, temp;
ListNode *p, *q;
p = beg;
q = p->next;
while(q != end)
{
if(q->val < key)//让p之前的数都小于key,p和q之间的数都大于等于key
{
p = p->next;
temp = q->val;
q->val = p->val;
p->val = temp;
}
q = q->next;
}
temp = beg->val;
beg->val = p->val;
p->val = temp;
return p;
}
void QickSort(ListNode *beg, ListNode *end)
{
if(beg != end)
{
ListNode * partion = Partion(beg, end);
QickSort(beg, partion);
QickSort(partion->next, end);
}
}