STL学习——list中的sort算法

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 1

using 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 1

using 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 ;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值