单链表快速排序算法实现

单链表结构:

typedef int ElemType;
typedef struct ListNode* List;

struct ListNode
{
	ElemType key
	List     next;
};

实现代码如下:

void QuickSort( List head, List tail )
{
	if ( head->next == tail || head->next->next == tail )
		return;

	List mid = head->next;
	List p = head;
	List q = mid;
	ElemType pivot = mid->key;
	List t = mid->next;
	
	while ( t != tail )
	{
		if ( t->key < pivot )
			p = p->next = t;
		else
			q = q->next = t;
		t = t->next;
	}
	p->next = mid;
	q->next = tail;

	QuickSort( head, mid );
	QuickSort( mid, tail );
}

调用时参数head为(带头节点的)单链表头节点指针,tail为单链表尾节点的next指针指向的类容。通常情况下tail的内容为NULL,因此,调用时为QuickSort(head, NULL)。

    原文作者:排序算法
    原文地址: https://blog.csdn.net/yangalbert/article/details/7577782
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞