1、数据结构排序测试sorting数据结构排序测试sorting一、 头(head)文件1. Utility#pragma once#include#include#include#include#include#include#include#include#include#include#includeusing namespace std;enum Error_codesuccess,fail,overflow,underflow,range_over,not_present;class Keyprivate: int key;public: static int comparisons;
2、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 &x,const Key &y);bool operator (const Key &x,const Key &y);bool operator = (const Key &x,const Key &y);bool operator = (const Key &x,co
3、nst Key &y);bool operator != (const Key &x,const Key &y);typedef Key Record;class Timerprivate: clock_t start_time;public: Timer() start_time=clock(); double elapsed_time() clock_t end_time=clock(); return (double)(end_time-start_time)/(double)CLK_TCK); void reset() start_time=clock(); ;class Random
4、private: int number;public: Random() number=0; int random_integer(int max) number=rand()%max; return number; ;2. List#pragma once#includeutility.h#define max_list 4000templateclass Listprotected: int count; List_entry entrymax_list;public: List(); int size() const; bool full() const; bool empty() co
5、nst; void clear(); void traverse(void (*visit)(List_entry &); Error_code retrieve(int position,List_entry & x) const; Error_code replace(int position,const List_entry & x); Error_code remove(int position,List_entry & x); Error_code insert(int position,const List_entry & x);templateList:List() count=
6、0;templateint List:size() const return count;templatebool List:full() const return count=max_list;templatebool List:empty() const return count=0;templatevoid List:clear() count=0; return;templatevoid List:traverse(void (*visit)(List_entry &) for(int i=0;icount;i+) (*visit)(entryi);templateError_code
7、 List:retrieve(int position, List_entry & x) const if(positioncount-1) return range_over; else x=entryposition; return success; templateError_code List:replace(int position, const List_entry & x) if(positioncount-1) return range_over; else entryposition=x; return success; templateError_code List:rem
8、ove(int position, List_entry & x) if(positioncount-1) return range_over; else x=entryposition; for(int i=position;i=count-2;i+) entryi=entryi+1; count-; return success; templateError_code List:insert(int position, const List_entry & x) if(full() return overflow; if(positioncount) return range_over;
9、for(int i=count-1;i=position;i-) entryi+1=entryi; entryposition=x; count+; return success;3. Sortable_list#pragma once#includelist.h#includeutility.htemplateclass Sortable_list:public Listpublic: /sorting void insertion_sort();/插入排序 void selection_sort();/选择排序 void merge_sort();/归并排序 void quick_sort
10、();/快速排序 void heap_sort();/堆排序 void finput(string f_name);/表从文件中读入数据 void re_finput(string f_name);/表中重新读入数据private: /辅助函数- /selection sort int max_key(int low,int high);/找出lowhigh中最大的键,并返回键所在位置 void swap(int low,int high);/将low和high位置的Record进行交换 /merge sort void recursive_merge_sort(int low,int hig
11、h);/递归算法 void merge(int low,int high);/归并 /quick sort void recursive_quick_sort(int low,int high);/递归算法 int partition(int low,int high);/中央的数作为支点,分组 /heap sort void insert_heap(const Record ¤t,int low,int high); void build_heap();/建立堆;/-/插入排序的实现templatevoid Sortable_list:insertion_sort() int f
12、irst_unsorted; int position; Record current; for(first_unsorted=1;first_unsorted=count-1;first_unsorted+) position=first_unsorted; current=entryfirst_unsorted; if(entryfirst_unsorted0 & entryposition-1current); entryposition=current; return;/选择排序的实现templatevoid Sortable_list:selection_sort() for(int
13、 position=count-1;position0;position-) int max=max_key(0,position); swap(max,position); return;/归并排序的实现templatevoid Sortable_list:merge_sort() recursive_merge_sort(0,size()-1); return;/快速排序的实现templatevoid Sortable_list:quick_sort() recursive_quick_sort(0,size()-1); return;/堆排序的实现templatevoid Sortabl
14、e_list:heap_sort() Record current; int last_unsorted; build_heap(); for(last_unsorted=count-1;last_unsorted0;last_unsorted-) current=entrylast_unsorted; entrylast_unsorted=entry0; insert_heap(current,0,last_unsorted-1); return;/-templatevoid Sortable_list:finput(string f_name) int position=0; ifstre
15、am in_list(f_name); for(string s;getline(in_list,s);) int a; for(istringstream sin(s);sina;) insert(position,a); position+; return;templatevoid Sortable_list:re_finput(string f_name) int position=0; ifstream in_list(f_name); for(string s;getline(in_list,s);) int a; for(istringstream sin(s);sina;) re
16、place(position,a); position+; return;/-templateint Sortable_list:max_key(int low,int high) int largest=low,current; for(current=low+1;current=high;current+) if(entrylargestentrycurrent) largest=current; return largest;templatevoid Sortable_list:swap(int low,int high) Record temp; temp=entrylow; entr
17、ylow=entryhigh; entryhigh=temp; return;/-templatevoid Sortable_list:recursive_merge_sort(int low,int high) if(highlow) recursive_merge_sort(low,(low+high)/2); recursive_merge_sort(low+high)/2+1,high); merge(low,high); return;templatevoid Sortable_list:merge(int low,int high) Record *temp=new Recordh
18、igh-low+1;/定义一个新的长度为原长数组 int index=0; int index1=low,mid=(low+high)/2,index2=mid+1; while(index1=mid & index2=high) if(entryindex1entryindex2) tempindex+=entryindex1+; else tempindex+=entryindex2+; while(index1=mid) tempindex+=entryindex1+; while(index2=high) tempindex+=entryindex2+; for(index=low;i
19、ndex=high;index+) entryindex=tempindex-low;/复制到原来的表中 delete temp; return;/-templatevoid Sortable_list:recursive_quick_sort(int low,int high) int pivot_position; if(lowhigh) pivot_position=partition(low,high); recursive_quick_sort(low,pivot_position-1); recursive_quick_sort(pivot_position+1,high); re
20、turn;templateint Sortable_list:partition(int low,int high) Record pivot; int i,last_small; swap(low,(low+high)/2); pivot=entrylow; last_small=low; for(i=low+1;i=high;i+) if(entryipivot) last_small=last_small+1; swap(last_small,i); swap(low,last_small); return last_small;/-templatevoid Sortable_list:
21、insert_heap(const Record ¤t,int low,int high) int large; large=2*low+1; while(large=high) if(largehigh & entrylarge=entrylarge) break; else entrylow=entrylarge; low=large; large=2*low+1; entrylow=current; return;templatevoid Sortable_list:build_heap() int low; for(low=count/2-1;low=0;low-) Rec
22、ord current=entrylow; insert_heap(current,low,count-1); return;4. Test_list#pragma once#includelist.h#includesortable_list.h#includeutility.h/插入排序的性能测试void test_insertion_sort(Sortable_list & the_list);/选择排序的性能测试void test_selection_sort(Sortable_list & the_list);/归并排序的性能测试void test_merge_sort(Sortab
23、le_list & the_list);/快速排序的性能测试void test_quick_sort(Sortable_list & the_list);/堆排序的性能测试void test_heap_sort(Sortable_list & the_list);二、 CPP文件1. Utility#includeutility.h/Key类的实现-int Key:comparisons=0;int Key:assignments=0;void Key:initialize() comparisons=0; return;int Key:counter() return comparisons;Key:Key(int x) key=x;int Key:the_key() const return key;Key &Key:operator=(const Key &x) Key:assignments+;/赋值语句加1,记录移动次数 key=x.key; return *this;/-
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1