[Happy DSA] 如何对单链表进行快速排序

之前有一篇文章介绍了glib库中单双向链表的排序采用了归并排序,并且不需要额外的空间,这篇文章将介绍c-algorithms库中单双向链表的快速排序过程:

通常快速排序算法都是施行于数组,但它也可以作用于单链表(这里以单向链表为例)。

原理跟数组快速排序是一样的,先partition,然后再递归的解决子单链表的快速排序。
以下是从c-algorithms代码中摘下来的代码段:

static SListEntry *slist_sort_internal(SListEntry **list, 
                                       SListCompareFunc compare_func)
{
     SListEntry *pivot;
     SListEntry *rover;
     SListEntry *less_list, *more_list;
     SListEntry *less_list_end, *more_list_end;
     
     /* If there are less than two entries in this list, it is
     * already sorted */

     if (*list == NULL || (*list)->next == NULL) {
          return *list;
     }

     /* The first entry is the pivot */

     pivot = *list;

     /* Iterate over the list, starting from the second entry.  Sort
     * all entries into the less and more lists based on comparisons
     * with the pivot */

     less_list = NULL;
     more_list = NULL;
     rover = (*list)->next;

     while (rover != NULL) {
          SListEntry *next = rover->next;

          if (compare_func(rover->data, pivot->data) < 0) {

               /* Place this in the less list */

               rover->next = less_list;
               less_list = rover;

          } else {

               /* Place this in the more list */

               rover->next = more_list;
               more_list = rover;

          }

          rover = next;
     }

     /* Sort the sublists recursively */

     less_list_end = slist_sort_internal(&less_list, compare_func);
     more_list_end = slist_sort_internal(&more_list, compare_func);

     /* Create the new list starting from the less list */

     *list = less_list;

     /* Append the pivot to the end of the less list.  If the less list
     * was empty, start from the pivot */

     if (less_list == NULL) {
          *list = pivot;
     } else {
          less_list_end->next = pivot;
     }

     /* Append the more list after the pivot */

     pivot->next = more_list;

     /* Work out what the last entry in the list is.  If the more list was 
     * empty, the pivot was the last entry.  Otherwise, the end of the 
     * more list is the end of the total list. */

     if (more_list == NULL) {
          return pivot;
     } else {
          return more_list_end;
     }
}

void slist_sort(SListEntry **list, SListCompareFunc compare_func)
{
     slist_sort_internal(list, compare_func);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值