标签:
<pre class="cpp" name="code">void recursive_merge_sort(Node<Record> * &sub_list){ if (sub_list != NULL && sub_list->next != NULL) { Node<Record> *second_half = divide_from(sub_list); recursive_merge_sort(sub_list); recursive_merge_sort(second_half); sub_list = merge(sub_list, second_half); } }
Node<Record> * divide_from(Node<Record> *sub_list)//P346 /* Post: The list of nodes referenced by sub_list has been reduced to its first half, and a pointer to the first node in the second half of the sublist is returned. If the sublist has an odd number of entries, then its first half will be one entry larger than its second.*/ { Node<Record> *position, // traverses the entire list *midpoint, // moves at half speed of position to midpoint *second_half; if ((midpoint = sub_list) == NULL) return NULL; // List is empty. position = midpoint->next; while (position != NULL) { // Move position twice for midpoint's one move. position = position->next; if (position != NULL) { midpoint = midpoint->next; position = position->next; } } second_half = midpoint->next; midpoint->next = NULL; return second_half; }
Node<Record> * merge(Node<Record> *first, Node<Record> *second) { Node<Record> *last_sorted; // points to the last node of sorted list Node<Record> combined; // dummy first node, points to merged list last_sorted = &combined; while (first != NULL && second != NULL) { // Attach node with smaller key if (first->entry <= second->entry) { last_sorted->next = first; last_sorted = first; first = first->next; // Advance to the next unmerged node. } else { last_sorted->next = second; last_sorted = second; second = second->next; } } // After one list ends, attach the remainder of the other. if (first == NULL) last_sorted->next = second; else last_sorted->next = first; return combined.next; }
void Sortable_list :: quick_sort( ) /* Post: The entries of the Sortable list have been rearranged so that their keys are sorted into nondecreasing order. Uses: Contiguous List implementation of Chapter 6, recursive quick sort .*/ { recursive_quick_sort(0, count - 1); }
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: Contiguous List implementation of Chapter 6, recursive quick sort , and partition . */ { int pivot_position; if (low < high) { // Otherwise, no sorting is needed. pivot_position = partition(low, high); recursive_quick_sort(low, pivot_position - 1); recursive_quick_sort(pivot_position + 1, high); } }
int Sortable_list :: partition(int low, int high) { 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]; // First entry is now pivot . last_small = low; //初始化为最小的位置 for (i = low + 1; i <= high; i++) if (entry[i] < pivot) { last_small ++; //从low+1开始放置。 swap(last_small, i); // Move large entry to right and small to left. } swap(low, last_small); // Put the pivot into its proper position. return last_small; }
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 implementation of Chapter 6,build_heap , and 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 last entry from list. entry[last_unsorted] = entry[0]; // Move top of heap to the end insert_heap(current, 0, last_unsorted - 1); // Restore the heap } }
void Sortable_list :: insert_heap(const Record ¤t, int low, int high) { 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 < high && entry[large] < entry[large + 1]) large++; // large is now the child of low with the largest key. if (current >= 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; }
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 implementation of Chapter 6, and insert heap . */ { int low; // All entries beyond the position low form a heap. //非叶子结点K的满足的条件是:2K+1<=COUNT-1 //K<=1/2*COUNT-1 for (low = count/2 - 1; low >= 0; low--) { Record current = entry[low]; insert_heap(current, low, count - 1); } }
void Sortable_list :: selection_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: max_key ,swap . */ { for (int position = 0; position < count-1; position++) { int min = min_key(position, count-1); swap(min, position); } }
int Sortable_list :: min_key(int low, int high) { int min, current; min = low; for (current = low + 1; current <= high; current++) if (entry[min] > entry[current]) min = current; return min; }
void Sortable_list :: swap(int low, int high) /* Pre: low and high are valid positions in the Sortable list . Post: The entry at position low is swapped with the entry at position high . Uses: The contiguous List implementation of Chapter 6. */ { Record temp; temp = entry[low]; entry[low] = entry[high]; entry[high] = temp; }
void Sortable_list :: selection_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: max_key ,swap . */ { for (int position = count - 1; position > 0; position--) { int max = max_key(0, position); swap(max, position); } }
int Sortable_list :: max_key(int low, int high) /* Pre: low and high are valid positions in the Sortable list and low <= high . Post: The position of the entry between low and high with the largest key is returned. Uses: The class Record . The contiguous List implementation of Chapter 6. */ { int largest, current; largest = low; for (current = low + 1; current <= high; current++) if (entry[largest] < entry[current]) largest = current; return largest; }
void Sortable_list :: shell_sort( ) /* Post: The entries of theSortable 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/3 + 1; for (start = 0; start < increment; start++) sort_interval(start, increment); // modified insertion sort } while (increment > 1); }
void Sortable_list :: sort_interval(int start, int increment){ int first_unsorted; // position of first unsorted entry int position; // searches sorted part of list Record current; // holds the entry temporarily removed from list for (first_unsorted = start + increment; first_unsorted < count; first_unsorted += increment ) if (entry[first_unsorted] < entry[first_unsorted - increment]) { position = first_unsorted; current = entry[first_unsorted]; // Pull unsorted entry out of the list. do { // Shift all entries until the proper position is found. entry[position] = entry[position - increment]; position -= increment; // position is empty. } while (position > start && entry[position - increment] > current); entry[position] = current; } } //上述算法和插
void Mergesort(Sortable_list & mylist) { Sortable_list secondlist; if (mylist.size()>1) { divide_from(mylist, secondlist); Mergesort(mylist); Mergesort(secondlist); combine(mylist, secondlist); } }
void divide_from(Sortable_list & mylist, Sortable_list & secondlist){ int mid=(mylist.size()-1)/2; //分割点的坐标 int secondsize=mylist.size()-(mid+1); //子序列二的长度 for (int i=0; i<secondsize; i++){ Record x; if(mylist.retrieve(mid+1, x)==success){ secondlist.insert(i, x); mylist.remove(mid+1, x);//在原序列中删除该节点 } } }
void combine(Sortable_list & firstsortlist, const Sortable_list & secondsortlist){ Sortable_list tmp; int m=0, n=0, i=0;//m为第一个列表的下标,n为第二个列表的下标 while(m<firstsortlist.size() && n<secondsortlist.size()){ Record x, y; firstsortlist.retrieve(m, x); secondsortlist.retrieve(n, y); if(x<=y){ tmp.insert(i++, x);//i为合并后列表的下标 m++; } else{ tmp.insert(i++, y); n++; } } <p><span style="color:black;">while(m<</span><span style="color:black;">firstsortlist.size</span><span style="color:black;">()){</span></p><p><span style="color:black;"> </span>Record x;</p><p><span style="color:black;"> </span><span style="color:black;">firstsortlist.retrieve</span><span style="color:black;">(m, x);</span></p><p><span style="color:black;"> </span><span style="color:black;">tmp.insert</span><span style="color:black;">(</span><span style="color:black;">i</span><span style="color:black;">++, x);</span></p><p><span style="color:black;"> </span>m++;</p><p><span style="color:black;"> </span>}</p><p><span style="color:black;"> </span>while(n<<span style="color:black;">secondsortlist.size</span><span style="color:black;">()){</span></p><p><span style="color:black;"> </span>Record y;</p><p><span style="color:black;"> </span><span style="color:black;">secondsortlist.retrieve</span><span style="color:black;">(n, y);</span></p><p><span style="color:black;"> </span><span style="color:black;">tmp.insert</span><span style="color:black;">(</span><span style="color:black;">i</span><span style="color:black;">++, y);</span></p><p><span style="color:black;"> </span>n++;</p><p><span style="color:black;"> </span>}</p><p><span style="color:black;"> </span><span style="color:black;">firstsortlist</span><span style="color:black;">=</span><span style="color:black;">tmp</span><span style="color:black;">;</span></p><p><span style="color:black;">}</span></p><pre class="cpp" name="code">class Sortable_list: public List<Record> { public: // Add prototypes for sorting methods here. void insertion_sort( ); private: // Add prototypes for auxiliary functions here. }; //对sub_list进行合并排序 void recursive_merge_sort(Node<Record> * &sub_list); //将sub_list划分为两部分,函数返回后半部分的指针 Node<Record> * divide_from(Node<Record> *sub_list); //将first和Second合并 Node<Record> * merge(Node<Record> *first, Node<Record> *second);
标签:
原文地址:http://blog.csdn.net/yuchenchenyi/article/details/51331588