基于单链表的快速排序


struct ListNode {
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};

void qsortList(ListNode* begin, ListNode* end)
{
	if(begin==NULL||begin==end) return;
	ListNode* index = begin;
	ListNode* pnode = begin->next;
	ListNode* end1 = index;
	while(pnode != end)
	{
		if(pnode->val < begin->val)
		{
			end1 = index;
			index=index->next;
			swap(index->val, pnode->val);
		}
		pnode = pnode->next;
	}
	if(pnode->val < begin->val)
	{
		end1 = index;
		index=index->next;
		swap(index->val,pnode->val);
	}
	swap(index->val, begin->val);

	if(begin!=index)
		qsortList(begin, end1);
	if(index != end)
	{
		ListNode* begin2=index->next;
		qsortList(begin2, end);
	}
}




你可能感兴趣的:(list,快速排序,单链表,qsort)