STL学习——list中的sort算法

来源:互联网 发布:网络直播行业研究报告 编辑:程序博客网 时间:2024/06/05 11:00

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> couter[64];    int fill =0 ;    while(!empty()){                    carry.splice(carry.begin(),*this,begin());                    int i = 0;                    //个人觉得couter[i].empty()这里是个点睛之笔,非常经典,理解了二进制从低位往高位进位的过程。我甚至感觉没有i<fill存在的必要。                    while(i < fill && !couter[i].empty()){                              couter[i].merger(carry);                             carry.swap(counter[i++];                    }                    carry.swap(counter[i]);                    if(i == fill) ++fill;    }        for(int i=1; i< fill; ++i)    couter[i].merge(couter[i-1]);    swap(couter[fill-1]);}


根据自己的理解将while循环去掉后,排序代码仍能运行正常,代码如下所示:

#include<iostream>#include<algorithm>#include<vector>#include<sstream>#include<string>#include<fstream>#include<list>#include<set>#define DEBUG 1using namespace std;void listsort(list<int> &lst){     list<int>::iterator ibeg, iend;     ibeg = lst.begin();     iend = lst.end();     if(ibeg == iend || ++ibeg == iend)             return ;          list<int> carry;     list<int> count[64] ;          #ifdef DEBUG     cout << "listsort:While Begin" << endl ;     #endif     while(!lst.empty())     {              int i = 0 ;              carry.splice(carry.begin(),lst, lst.begin());              while(!count[i].empty())              {                      count[i].merge(carry);                      carry.swap(count[i++]) ;                                      }                carry.swap(count[i]);                                                          }     #ifdef DEBUG     cout << "listsort:While End" << endl ;     #endif          int ix;     for(ix=1; ix<64; ++ix)             count[ix].merge(count[ix-1]);     lst.swap(count[63]) ;         }int main(void){    int a[] = {3,2,2,9,5,7,2,4,5};    int size = sizeof(a)/ sizeof(int);         list<int> mylst(a,a+size) ;    listsort(mylst) ;         for(list<int>::iterator iter = mylst.begin(); iter != mylst.end(); ++iter)    {                cout << *iter << "," ;                            }    cout << endl ;system("pause");return 0 ;}


 

另自己写了递归的快排算法:

#include<iostream>#include<algorithm>#include<vector>#include<sstream>#include<string>#include<fstream>#include<list>#include<set>#define DEBUG 1using namespace std;template<class T>void swap(T &l, T&r){     T temp = l;     l = r;     r= temp ;     } template<class T>void printArray(T *a, int size){     for(int i=0; i!=size; ++i)             cout << *(a+i) << "," ;     cout << endl ;     }template<class T>void quicksort(T *at, int l, int r){     if(l >= r) return;     int temp = at[l];     int templ = l ;     int tempr = r;          #ifdef DEBUG     cout << "While:Begin" << endl ;     #endif          while(l < r)     {       while( at[r] >= temp && l < r ) --r;       at[l] = at[r] ;              while(at[l] <=temp && l < r) ++l;       at[r] = at[l] ;            }          #ifdef DEBUG     cout << "While:End" << endl ;     #endif          at[l] = temp ;     quicksort(at,templ,l);     quicksort(at,l+1,tempr);     }int main(void){    int a[] = {3,2,2,9,5,7,2,4,5};    int size = sizeof(a)/ sizeof(int);     quicksort(a,0,size-1);    printArray(a,size);system("pause");return 0 ;}

原创粉丝点击