为了正常的体验网站,请在浏览器设置里面开启Javascript功能!

归并排序

2017-09-27 5页 doc 18KB 8阅读

用户头像

is_279425

暂无简介

举报
归并排序归并排序 #include #include #include #include #include using namespace std; template class CMP //抽象操作符类 { public: virtual bool operator()(const T&one,const T&other)=0; //纯虚函数,重载操作符() }; template class LessThan:public CMP //小于操作符类 { public: bool operator()(co...
归并排序
归并排序 #include #include #include #include #include using namespace std; template class CMP //抽象操作符类 { public: virtual bool operator()(const T&one,const T&other)=0; //纯虚函数,重载操作符() }; template class LessThan:public CMP //小于操作符类 { public: bool operator()(const T&one,const T&other) { return one class GreatThan:public CMP { public: bool operator()(const T&one,const T&other) { return one>other; } }; template //待排序元素的类型 class Sort //抽象排序类 { protected: Iterator array; //指向待排序区间的开始指针 public: virtual void operator()(const Iterator &beg,const Iterator &end,CMP &cmp)=0; }; template class MergeSort:public Sort { private: int low,high; //指向待排序区间开始和结束 void Merge(int s,int t,int p,int q,CMP&cmp); //将区间[s...t],[p...q]归并为有序区间 void Sort_Function(int low,int high,CMP&cmp); //将区间[low...high]归并排序 public: void operator()(const Iterator &beg,const Iterator &end,CMP &cmp) { low=0; high=end-beg-1; array=beg; Sort_Function(low,high,cmp); } }; template void MergeSort::Merge(int s,int t,int p,int q,CMP &cmp) { int len=q-s+1; T *tmp=new T[len]; int i,j,k; i=s; j=p; k=0; while(i<=t && j<=q) { if(cmp(array[i],array[j])) tmp[k]=array[i++]; else tmp[k]=array[j++]; k++; } while(i<=t) { tmp[k++]=array[i++]; } while(j<=q) { tmp[k++]=array[j++]; } for(i=s,k=0;k void MergeSort::Sort_Function(int low,int high,CMP &cmp) { if(lowMerge(low,mid,mid+1,high,cmp); } } template void Merge_Sort(const Iterator &beg,const Iterator &end,CMP&cmp) { MergeSort ms; ms(beg,end,cmp); } void main() { vectora; int x; srand((unsigned)time(0)); const int n=80; for(int i=0;i::iterator,int>(a.begin(),a.end(),GreatThan()); Merge_Sort::iterator,int>(a.begin(),a.end(),LessThan()); copy(a.begin(),a.end(),ostream_iterator(cout," ")); cout<
/
本文档为【归并排序】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。 本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。 网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。

历史搜索

    清空历史搜索