最近在读《STL源码剖析》,颇有收获。当看到list结构的排序方法sort时,发现侯捷先生点到为止,说采用的是快速排序,也没有继续说明。我心存疑虑,怎么看这个代码都不像快排。
template <class _Tp, class _Alloc> template <class _StrictWeakOrdering>
void list<_Tp, _Alloc>::sort(_StrictWeakOrdering __comp)
{
// Do nothing if the list has length 0 or 1.
if (_M_node->_M_next != _M_node && _M_node->_M_next->_M_next != _M_node) {
list<_Tp, _Alloc> __carry;
list<_Tp, _Alloc> __counter[64];
int __fill = 0;
while (!empty()) {
__carry.splice(__carry.begin(), *this, begin());
int __i = 0;
while(__i < __fill && !__counter[__i].empty()) {
__counter[__i].merge(__carry, __comp);
&nb

这篇博客探讨了C++ STL list的sort成员函数,通过分析源码和实验,揭示了其实现方式是归并排序。作者通过断点跟踪微软的实现,详细解释了排序过程,建议在理解复杂算法时,可以通过编写测试程序并调试来帮助理清思路。
最低0.47元/天 解锁文章
786

被折叠的 条评论
为什么被折叠?



