数据结构排序测试sorting.docx

上传人:b****5 文档编号:6503035 上传时间:2023-01-07 格式:DOCX 页数:21 大小:18.79KB
下载 相关 举报
数据结构排序测试sorting.docx_第1页
第1页 / 共21页
数据结构排序测试sorting.docx_第2页
第2页 / 共21页
数据结构排序测试sorting.docx_第3页
第3页 / 共21页
数据结构排序测试sorting.docx_第4页
第4页 / 共21页
数据结构排序测试sorting.docx_第5页
第5页 / 共21页
点击查看更多>>
下载资源
资源描述

数据结构排序测试sorting.docx

《数据结构排序测试sorting.docx》由会员分享,可在线阅读,更多相关《数据结构排序测试sorting.docx(21页珍藏版)》请在冰豆网上搜索。

数据结构排序测试sorting.docx

数据结构排序测试sorting

数据结构排序测试sorting

一、头(head)文件

1.Utility

#pragmaonce

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

usingnamespacestd;

enumError_code{success,fail,overflow,underflow,range_over,not_present};

classKey

{

private:

intkey;

public:

staticintcomparisons;

staticintassignments;

staticvoidinitialize();

staticintcounter();

Key(intx=0);

intthe_key()const;

Key&operator=(constKey&y);

};

booloperator==(constKey&x,constKey&y);

booloperator>(constKey&x,constKey&y);

booloperator<(constKey&x,constKey&y);

booloperator>=(constKey&x,constKey&y);

booloperator<=(constKey&x,constKey&y);

booloperator!

=(constKey&x,constKey&y);

typedefKeyRecord;

classTimer

{

private:

clock_tstart_time;

public:

Timer()

{

start_time=clock();

}

doubleelapsed_time()

{

clock_tend_time=clock();

return((double)(end_time-start_time))/((double)CLK_TCK);

}

voidreset()

{

start_time=clock();

}

};

classRandom

{

private:

intnumber;

public:

Random()

{

number=0;

}

intrandom_integer(intmax)

{

number=rand()%max;

returnnumber;

}

};

2.List

#pragmaonce

#include"utility.h"

#definemax_list4000

template

classList

{

protected:

intcount;

List_entryentry[max_list];

public:

List();

intsize()const;

boolfull()const;

boolempty()const;

voidclear();

voidtraverse(void(*visit)(List_entry&));

Error_coderetrieve(intposition,List_entry&x)const;

Error_codereplace(intposition,constList_entry&x);

Error_coderemove(intposition,List_entry&x);

Error_codeinsert(intposition,constList_entry&x);

};

template

List:

:

List()

{

count=0;

}

template

intList:

:

size()const

{

returncount;

}

template

boolList:

:

full()const

{

returncount==max_list;

}

template

boolList:

:

empty()const

{

returncount==0;

}

template

voidList:

:

clear()

{

count=0;

return;

}

template

voidList:

:

traverse(void(*visit)(List_entry&))

{

for(inti=0;i

(*visit)(entry[i]);

}

template

Error_codeList:

:

retrieve(intposition,List_entry&x)const

{

if(position<0||position>count-1)returnrange_over;

else

{

x=entry[position];

returnsuccess;

}

}

template

Error_codeList:

:

replace(intposition,constList_entry&x)

{

if(position<0||position>count-1)returnrange_over;

else

{

entry[position]=x;

returnsuccess;

}

}

template

Error_codeList:

:

remove(intposition,List_entry&x)

{

if(position<0||position>count-1)returnrange_over;

else

{

x=entry[position];

for(inti=position;i<=count-2;i++)

entry[i]=entry[i+1];

count--;

returnsuccess;

}

}

template

Error_codeList:

:

insert(intposition,constList_entry&x)

{

if(full())returnoverflow;

if(position<0||position>count)returnrange_over;

for(inti=count-1;i>=position;i--)

entry[i+1]=entry[i];

entry[position]=x;

count++;

returnsuccess;

}

3.Sortable_list

#pragmaonce

#include"list.h"

#include"utility.h"

template

classSortable_list:

publicList

{

public:

//sorting

voidinsertion_sort();//插入排序

voidselection_sort();//选择排序

voidmerge_sort();//归并排序

voidquick_sort();//快速排序

voidheap_sort();//堆排序

voidfinput(stringf_name);//表从文件中读入数据

voidre_finput(stringf_name);//表中重新读入数据

private:

//辅助函数--------------------------------------------------

//selectionsort

intmax_key(intlow,inthigh);//找出low~high中最大的键,并返回键所在位置

voidswap(intlow,inthigh);//将low和high位置的Record进行交换

//mergesort

voidrecursive_merge_sort(intlow,inthigh);//递归算法

voidmerge(intlow,inthigh);//归并

//quicksort

voidrecursive_quick_sort(intlow,inthigh);//递归算法

intpartition(intlow,inthigh);//中央的数作为支点,分组

//heapsort

voidinsert_heap(constRecord¤t,intlow,inthigh);

voidbuild_heap();//建立堆

};

//--------------------------------------------------------------

//插入排序的实现

template

voidSortable_list:

:

insertion_sort()

{

intfirst_unsorted;

intposition;

Recordcurrent;

for(first_unsorted=1;first_unsorted<=count-1;first_unsorted++)

{

position=first_unsorted;

current=entry[first_unsorted];

if(entry[first_unsorted]

{

do

{

entry[position]=entry[position-1];

position--;

}while(position>0&&entry[position-1]>current);

}

entry[position]=current;

}

return;

}

//选择排序的实现

template

voidSortable_list:

:

selection_sort()

{

for(intposition=count-1;position>0;position--)

{

intmax=max_key(0,position);

swap(max,position);

}

return;

}

//归并排序的实现

template

voidSortable_list:

:

merge_sort()

{

recursive_merge_sort(0,size()-1);

return;

}

//快速排序的实现

template

voidSortable_list:

:

quick_sort()

{

recursive_quick_sort(0,size()-1);

return;

}

//堆排序的实现

template

voidSortable_list:

:

heap_sort()

{

Recordcurrent;

intlast_unsorted;

build_heap();

for(last_unsorted=count-1;last_unsorted>0;last_unsorted--)

{

current=entry[last_unsorted];

entry[last_unsorted]=entry[0];

insert_heap(current,0,last_unsorted-1);

}

return;

}

//------------------------------------------------------------

template

voidSortable_list:

:

finput(stringf_name)

{

intposition=0;

ifstreamin_list(f_name);

for(strings;getline(in_list,s);)

{

inta;

for(istringstreamsin(s);sin>>a;)

{

insert(position,a);

position++;

}

}

return;

}

template

voidSortable_list:

:

re_finput(stringf_name)

{

intposition=0;

ifstreamin_list(f_name);

for(strings;getline(in_list,s);)

{

inta;

for(istringstreamsin(s);sin>>a;)

{

replace(position,a);

position++;

}

}

return;

}

 

//------------------------------------------------------

template

intSortable_list:

:

max_key(intlow,inthigh)

{

intlargest=low,current;

for(current=low+1;current<=high;current++)

{

if(entry[largest]

largest=current;

}

returnlargest;

}

template

voidSortable_list:

:

swap(intlow,inthigh)

{

Recordtemp;

temp=entry[low];

entry[low]=entry[high];

entry[high]=temp;

return;

}

//--------------------------------------------------------------

template

voidSortable_list:

:

recursive_merge_sort(intlow,inthigh)

{

if(high>low)

{

recursive_merge_sort(low,(low+high)/2);

recursive_merge_sort((low+high)/2+1,high);

merge(low,high);

}

return;

}

template

voidSortable_list:

:

merge(intlow,inthigh)

{

Record*temp=newRecord[high-low+1];//定义一个新的长度为原长数组

intindex=0;

intindex1=low,mid=(low+high)/2,index2=mid+1;

while(index1<=mid&&index2<=high)

{

if(entry[index1]

temp[index++]=entry[index1++];

else

temp[index++]=entry[index2++];

}

while(index1<=mid)

temp[index++]=entry[index1++];

while(index2<=high)

temp[index++]=entry[index2++];

for(index=low;index<=high;index++)

{

entry[index]=temp[index-low];//复制到原来的表中

}

deletetemp;

return;

}

//-------------------------------------------------------------

template

voidSortable_list:

:

recursive_quick_sort(intlow,inthigh)

{

intpivot_position;

if(low

{

pivot_position=partition(low,high);

recursive_quick_sort(low,pivot_position-1);

recursive_quick_sort(pivot_position+1,high);

}

return;

}

template

intSortable_list:

:

partition(intlow,inthigh)

{

Recordpivot;

inti,last_small;

swap(low,(low+high)/2);

pivot=entry[low];

last_small=low;

for(i=low+1;i<=high;i++)

{

if(entry[i]

{

last_small=last_small+1;

swap(last_small,i);

}

}

swap(low,last_small);

returnlast_small;

}

//----------------------------------------------------

template

voidSortable_list:

:

insert_heap(constRecord¤t,intlow,inthigh)

{

intlarge;

large=2*low+1;

while(large<=high)

{

if(large

large++;

if(current>=entry[large])

break;

else

{

entry[low]=entry[large];

low=large;

large=2*low+1;

}

}

entry[low]=current;

return;

}

template

voidSortable_list:

:

build_heap()

{

intlow;

for(low=count/2-1;low>=0;low--)

{

Recordcurrent=entry[low];

insert_heap(current,low,count-1);

}

return;

}

4.Test_list

#pragmaonce

#include"list.h"

#include"sortable_list.h"

#include"utility.h"

//插入排序的性能测试

voidtest_insertion_sort(Sortable_list&the_list);

//选择排序的性能测试

voidtest_selection_sort(Sortable_list&the_list);

//归并排序的性能测试

voidtest_merge_sort(Sortable_list&the_list);

//快速排序的性能测试

voidtest_quick_sort(Sortable_list&the_list);

//堆排序的性能测试

voidtest_heap_sort(Sortable_list&the_list);

二、CPP文件

1.Utility

#include"utility.h"

//Key类的实现----------------------------------------

intKey:

:

comparisons=0;

intKey:

:

assignments=0;

voidKey:

:

initialize()

{

comparisons=0;

return;

}

intKey:

:

counter()

{

returncomparisons;

}

Key:

:

Key(intx)

{

key=x;

}

intKey:

:

the_key()const

{

returnkey;

}

Key&Key:

:

operator=(constKey&x)

{

Key:

:

assignments++;//赋值语句加1,记录移动次数

key=x.key;

return*this;

}

//---------------------------------------

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 幼儿教育 > 育儿知识

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1