ITPub博客

首页 > 应用开发 > IT综合 > 算法单链表快速排序算法实现

算法单链表快速排序算法实现

IT综合 作者:dongyu2013 时间:2014-04-15 16:49:34 0 删除 编辑
  1. typedef int ElemType;  
  2. typedef struct ListNode* List;  
  3.   
  4. struct ListNode  
  5. {  
  6.     ElemType key  
  7.     List     next;  
  8. };  

实现代码如下:



  1. void QuickSort( List head, List tail )  
  2. {  
  3.     if ( head->next == tail || head->next->next == tail )  
  4.         return;  
  5.   
  6.     List mid = head->next;  
  7.     List p = head;  
  8.     List q = mid;  
  9.     ElemType pivot = mid->key;  
  10.     List t = mid->next;  
  11.       
  12.     while ( t != tail )  
  13.     {  
  14.         if ( t->key < pivot )  
  15.             p = p->next = t;  
  16.         else  
  17.             q = q->next = t;  
  18.         t = t->next;  
  19.     }  
  20.     p->next = mid;  
  21.     q->next = tail;  
  22.   
  23.     QuickSort( head, mid );  
  24.     QuickSort( mid, tail );  
  25. }  


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

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29012686/viewspace-1142673/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2013-12-25

  • 博文量
    263
  • 访问量
    238071