基于list实现一个快速排序算法模板

list::splice

Transfer elements from list to list.Transfers elements from x into the container, inserting them at position.

关键信息应该是Transfer和inserting them at position,也就是说会把一个链表的节点转移到另一个链表,并把转移过来的节点插入到指定的位置

The first version (1) transfers all the elements of x into the container.

The second version (2) transfers only the element pointed by i from x into the container.

The third version (3) transfers the range [first,last) from x into the container.

std::partition

Rearranges the elements from the range [first,last), in such a way that all the elements for which pred returns true precede all those for which it returns false.The iterator returned points to the first element of the second group.

关键信息是会根据pred的返回值进行rearranes,所有返回true的元素在所有返回false的元素前面,但相对顺序和调用前可能就不一样了。如果向保持相对顺序,可使用std::stable_partition

Possible output:

odd elements: 1 9 3 7 5

even elements: 6 4 8 2

QuickSort Algorithm

快速排序算法是初学编程就会接触到的基本算法,如下图所示

enter image description here

Using splice and partition to implemate the quicksort algorithm

  • 发表于:
  • 原文链接http://kuaibao.qq.com/s/20180403G13BWD00?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码关注腾讯云开发者

领取腾讯云代金券