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 ;}
- STL学习——list中的sort算法
- STL中的list::sort算法解析
- STL源码分析--list中的sort算法
- STL list::sort算法
- 【STL】算法 — sort
- STL中的Sort 算法
- STL中的Sort算法
- SGI STL序列式容器list中的sort算法
- SGI STL: list::sort()算法
- STL源码剖析——list容器的排序算法sort()
- STL源码剖析——list容器的排序算法sort()
- stl中list的sort算法实现
- stl::list自带的sort算法
- STL源码系列--List::sort算法解析
- list不能使用STL算法sort()
- stl中list的sort算法实现
- [STL源码剖析] list中的sort()函数
- SGI-STL学习笔记之list::sort()
- uva 10719 - Quotient Polynomial
- 毕业两年,需要下一个决心做选择
- javaBean多去少补
- WCF RIA方法无效的一种解决方法
- 关于hibernate纯sql查询返回结果集的问题(hbm.xml中不写多表关联)
- STL学习——list中的sort算法
- java中finally关键字的用处
- 德沃夏克布局与快蹄布局的手指负荷统计
- android消息推送-XMPP
- Mysql学习3——查看表结构、修改和删除数据表
- 2.4 存储管理
- java se TreeSet自己实现Comparator接口
- C语言中long long的用法
- 黑马程序员_JAVA面向对象三大基本特征