单链表快速排序算法实现

来源:互联网 发布:小熊电蒸锅 淘宝 编辑:程序博客网 时间:2024/06/06 09:14

单链表结构:

typedef int ElemType;typedef struct ListNode* List;struct ListNode{ElemType keyList     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;elseq = 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)。


原创粉丝点击