06 2013 档案
摘要:归并排序理论上时间复杂度只有O(NlogN),但是其中存在过多临时内存分配和copy操作而不适用于内存排序,却是外部排序的基本思路。下面是我的实现:#include<iostream>#include<algorithm>#include<cstring>using namespace std;int g=0;/*a,b is input array * c is output array * size of c is na+nb */template<typename T>void merge(T a[],int na,T b[],int nb
阅读全文
摘要:快排的思路就不说了,中轴分组,借助一个临时单元。时间复杂度O(NlogN)。下面是实现:#include<iostream>using namespace std;template<typename T>bool _less(T a,T b){ return a<b;}template<typename T,typename F>void qsort(T a[],int n,F f=_less<T>){ if(n<2)return; int first=0,last=n-1; T temp=a[0]; while(first!=las
阅读全文
摘要:基数排序是基于桶式排序的变种,主要是为了通过多次遍历减少空间占用(效果相当显著)。其思路可以描述为: 1.首先待排序的序列要满足基数排序的特征,比如有“基数”的概念(整数和字符串都可以套用) 2.建立一个大小为“基数”的数组,数组每个元素是一个队列(为了节省空间应该使用链表实现) 3.根据序列的“位数”决定遍历的次数m,每次遍历相当于按某一位做桶式排序。重复的元素push到队列中。 4.m次遍历后排序完成,最后一次遍历输出结果。因为数据不是按线性结构存储。 下面是我的实现,采用vector<list>结构: 1 #include<iostream> 2 #incl...
阅读全文
摘要:希尔排序的时间复杂度在O(N)到O(N2)之间,在大量数据排序中实践性能超过堆排序。特点是算法简单但是分析复杂。下面是实现:/* * a[] is an array to be sorted * n1 is the T array length * inc[] is the array to indecate the increasement * n2 is the inc array length */template<typename T>void shellsort(T a[],int n1,int inc[],int n2){ for(int i=0;i<n2;++i
阅读全文

浙公网安备 33010602011771号