08年9月入学,12年7月毕业,结束了我在软件学院愉快丰富的大学生活。此系列是对四年专业课程学习的回顾,索引参见:http://blog.csdn.net/xiaowei_cqu/article/details/7747205
template
Sortable_list::Sortable_list(const Record A[],int size)
{
for(int i=0;i
1.编写函数shell_sort。函数中我们从首先定义increment=0,观察题目要求,可以得到在循环中有这样的关系increment=increment/2;使用循环将每次分组后的每组排列,排列我们再增加函数sort_interval();在每组的排序中使用的直接插入排序,所以可以这样实现sort_interval:每次定义一个临时的Sortable_list对象tmp记录每次分组的小组,对tmp使用insertion_sort,进而我们编写函数insertion_sort();
template
void Sortable_list::shell_sort()
/*Post: The entries of the Sortable_list have been rearranged so that the keys in
all the entries are sorted into nondecreasing order.
Uses: sort_interval.
*/
{
int increment, //spacing of entries in sublist
start; //starting point of sublist
increment=count;
do{
increment=increment/2;
for(start=0;start1);
}
template
void Sortable_list::sort_interval(int start,int increment)
{
Sortable_list temp;
int j=0;
for(int i=start;i
void Sortable_list::insertion_sort()
/*Post: The entries of the Sortable_list have been rearranged so that the keys in
all the entries are sorted into nondecreasing order.
Uses: Methods for the class Record; the contiguous List
*/
{
int first_unsorted; //position of first unsorted entry
int position; //searches sorted part of list
Record current; //holds the entry emporarily removed from list
for(first_unsorted=1;first_unsorted0&&entry[position-1]>current);
entry[position]=current;
}
}
//其他辅助函数见源文件
template
void Sortable_list::merge(int low,int high)
{
Record *tmp=new Record[high-low+1];
int index=0;
int index1=low,mid=(low+high)/2,index2=mid+1;
while(index1<=mid&&index2<=high){
if(entry[index1]
void Sortable_list::recursive_merge_sort(int low,int high)
/*Post: The entries of the sortable list between
index low and high have been rearranged so that
their keys are sorted into nondecreasing order.
Uses: The contiguous list
*/
{
if(high>low){
recursive_merge_sort(low,(high+low)/2);
recursive_merge_sort((high+low)/2+1,high);
merge(low,high);
}
}
template
void Sortable_list::merge_sort()
/* Post: The entries of the sortable list have been rearranged so that
their keys are sorted into nondecreasing order.
Uses: The contiguous list
*/
{
recursive_merge_sort(0,size()-1);
}
template
int Sortable_list::partition(int low,int high)
/*Pre: low and high are valid positions of the Sortable_list, with low<=high.
Post: The center (or left center) entry in the range between indices low and
high of the Sortable_list has been chosen as a pivot. All entries of the
Sortable_list between indices low and high, inclusive, have been rearranged
so that chosen with keys less than the pivot com before the pivot
and the remaining entries come after the pivot. The final position of the
pivot is returned.
Uses: swap(int,int j) contigious list
*/
{
Record pivot;
int i, //used to scan through the list
last_small; //position of the last key less than pivot
swap(low,(low+high)/2);
pivot=entry[low];
last_small=low;
for(i=low+1;i<=high;i++)
//At the beginning of each iteration of this loop, we have the following conditions:
// If low=pivot.
if(entry[i]
void Sortable_list::recursive_quick_sort(int low,int high)
/*Pre: low and high are valid positions in the Sortable list.
Post: The entries of the Sortable_list have been rearranged so that their keys
are sorted into nondecreasing order.
Uses: The contiguous list, recursive_quick_sort, partition.
*/
{
int pivot_postion;
if(low
void Sortable_list::quick_sort()
{
recursive_quick_sort(0,count-1);
}
//其他函数见源码
template
void Sortable_list::insert_heap(const Record ¤t,int low,int high)
/*Pre: The entries of the Sortable_list between indices low+1 and high,
inclusive,form a heap. The entry in position low will be discarded.
Post: The entry current has been inserted into the Sortabl_list and the entries,
rearranged so that the entries between indices low and high, inclusive
form a heap.
*/
{
int large; //position of child of entry[low] with the larger key
large=2*low+1; //large is now the left child of low
while(large<=high){
if(large=entry[large])
break; //current belongs in position low
else{ //Promote entry[large] and move down the tree
entry[low]=entry[large];
low=large;
large=2*low+1;
}
}
entry[low]=current;
traverse(print_out);
cout<
void Sortable_list::build_heap()
/*Post: The entries of the Sortable_list have been rearranged so that
it becomes a heap
Uses: The contiguous list and insert_heap
*/
{
int low;
for(low=count/2-1;low>=0;low--){
Record current=entry[low];
insert_heap(current,low,count-1);
}
}
template
void Sortable_list::heap_sort()
/*Post: The entries of the Sortable_list have been rearranged so that their keys
are sorted into nondecreasing order.
Uses: The contiguous list, build_heap, insert_heap
*/
{
Record current; //temporary storage for moving entries
int last_unsorted; //Entries beyond last_unsorted have been sorted.
build_heap(); //First phase: Turn the list into a heap
for(last_unsorted=count-1;last_unsorted>0;last_unsorted--){
current=entry[last_unsorted]; //Extract the last entry from the list
entry[last_unsorted]=entry[0]; //Move top of heap to the end
insert_heap(current,0,last_unsorted-1); //Restore the heap
}
}
//其他函数见源码