排序算法的实现和比较.docx
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_05.gif)
《排序算法的实现和比较.docx》由会员分享,可在线阅读,更多相关《排序算法的实现和比较.docx(8页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、实验七排序算法的实现和比拟1、List类的定义及实现方法运用顺序线性表,详见实验五List类的实现和应用2、Key类的定义及实现方法class Key int key;public:static int comparisons;static int assignments;static void initialize();static int counter();Key (int x = 0 );int the_key() const;Key &operator =(const Key &y); );bool operator =(const Key &y,const Key &x);bool
2、 operator !=(const Key &y,const Key &x);bool operator =(const Key &y,const Key &x);bool operator (const Key &y,const Key &x);bool operator =(const Key &x, const Key &y)(Kcy:comparisons+;return x.the_key() = y.the_key();)bool operator =(const Key &x, const Key &y)(Key:comparisons+;return x.thc_kcy()
3、(const Key &x, const Key &y)Key: :comparisons+;return x.the_key() y.the_key();1bool operator (const Key &x, const Key &y)(Key: :comparisons+;return x.the_key() y.the_key();)int Key:the_key() const(return key;13、Timer类的定义及函数实现 详见书本6804、Random类的定义及函数实现 详见书本6686715、utility.h 的内容#includeusing namespace
4、std;enumError_codesuccess,fail,rang_error,range_error,underflow,overflow,not_present;6、Sortablejist的类的定义及函数实现template class Sortablejist: public List public:Sortable_list();virtual-Sortable_list();void insertion_sort();void selcction_sort();void shell_sort();void quick_sort();void heap_sort();void m
5、erge_sort();private:void sort_interval(int start, int increment);void swap(int low, int high);int max_key(int low, int high);int partition(int low, int high);void recursive_quick_sort(int low, int high);void insert_heap(const Record ¤t, int low, int high);void build_heap();void recursive_merge
6、_sort(int low, int high);;/template 构造函数SortableistvRecord:Sortableist()template class Record析构函数Sortable_list:Sortable_list()()template 插入排序void Sortablc_list:insertion_sort()(int first_unsorted;int position;Record current;for(first_unsorted= l;first_unsortedcount;first_unsorted+) if(entryfirst_uns
7、orted0&entryposition-1 lcurrent);entryposition=cuncnt;template 选择排序 void Sortable_list:selection_sort()(for (int position = count - 1; position 0; position) (int max = max_key(0, position);swap(max, position);I)template void Sortable_list:swap(int low, int high)Record temp;temp = entry low;entryflow
8、l = entryfhighl;entryhighj = temp;1template int Sortable_list:max_key(int low, int high)(int largest, current;largest = low;fbr (current = low + 1; current = high; currcnt+)if (entryflargest entryfeurrent)largest = current;return largest;)templatevoid Sorlableist:selection_sort()(for(position=count-
9、1 ;position0;position-)(int max=max_key(0,position);swap(max,position);)1teniplateint max_key(int low,int high)int cuiTent;int largest=low;for(current=low+1 ;current=high;current+) if(entrylargestentrycurrent)largest二current;return largest;)lemplatevoid swap(int low,int high);Record temp;temp=entryl
10、ow;entry low=entry high;entryhighl=temp;)template 希尔排序void Sorlable_lisl:sort_inlerval(int start, int increment)(int first_unsorted; / position of first unsorted entryint place; / searches sorted part of listRecord current; / used to swap entriesfor (first_unsorted = start + increment; first_unsorte
11、d count;first_unsorled += increment)if (entryfirst_unsorted start & entrylplace - increment current); entryplace = current;I1template void Sortable_list:shell_sort()int increment, / spacing of entries in sublist start; / starting point of sublistincrement = count;do(increment = increment / 3 + 1;for
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 排序 算法 实现 比较
![提示](https://www.taowenge.com/images/bang_tan.gif)
限制150内