STL中list的排序算法

来源:互联网 发布:sql2008数据库备份 编辑:程序博客网 时间:2024/05/16 02:26

转-注:因为非random access的链表不适合做quick sort, 一直怀疑STL的list使用了merge sort. 今天在网上搜了一下,转至此处做个记录。

STL 中的List排序算法(in SGI STL)

SGI STL笔记(list中的sort算法)

template <class T, class Alloc>void list<T, Alloc>::sort() {  if (node->next == node || link_type(node->next)->next == node) return;  list<T, Alloc> carry;  list<T, 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);      carry.swap(counter[i++]);    }    carry.swap(counter[i]);             if (i == fill) ++fill;  }   for (int i = 1; i < fill; ++i) counter[i].merge(counter[i-1]);  swap(counter[fill-1]);}


0 0