归并排序
2015-09-29 17:38
218 查看
连续结构的归并排序:
链式结构的归并排序:
#include <iostream> #include <cstring> #include <cstdio> #include <cmath> #include <algorithm> #include <stack> using namespace std; template <typename T> void merge(T *a , int first, int mid, int last) { T* temp = new T[last - first + 1]; int first1 = first, last1 = mid; int first2 = mid + 1, last2 = last; int index = 0; while(first1 <= last1 && first2 <= last2){ if(a[first1 ] <= a[first2]) temp[index++] = a[first1++]; else temp[index++] = a[first2++]; } while(first1 <= last1){ temp[index++] = a[first1++]; } while(first2 <= last2){ temp[index++] = a[first2++]; } for(int i = first; i <= last; i++){ a[i] = temp[i - first]; } delete [] temp; return; } template<typename T> void MergeSort(T *a, int first, int last){ if(last - first > 0){ //at least two element int mid = (last + first) / 2; MergeSort(a, first, mid); MergeSort(a, mid + 1, last); merge(a, first, mid, last); } return; } #define MAXN 100 int a[MAXN]; int main() { for(int i = 0; i < MAXN; i++){ a[i] = MAXN-i; } MergeSort(a, 0, MAXN-1); for(int i = 0; i < MAXN; i++){ cout << a[i] << endl; } return 0; }
链式结构的归并排序:
#include <iostream> #include <cstring> #include <cstdio> using namespace std; template <typename Node_entry> struct Node{ Node_entry entry; Node* next; Node(){ next = NULL; } Node(const Node_entry& newEntry, Node* add_on = NULL){ entry = newEntry; next = add_on; } }; template<typename T> void PrintList(Node<T>* l){ if(l != NULL) cout << l->entry; l = l->next; while(l != NULL){ cout << "->" << l->entry; l = l->next; } cout << "->NULL\n"; return; } template <typename T> Node<T>* divide(Node<T>* list){ Node<T> *position = list->next,// traversed the entire list *second, *end_of_first = list; while(position != NULL){ position = position->next; if(position != NULL){ position = position->next; end_of_first = end_of_first->next; } } second = end_of_first->next; end_of_first->next = NULL; // add a NULL at the end of the first /* cout << "check divide\n"; cout << "first:\n"; PrintList(list); cout << "second:\n"; PrintList(second); */ return second; } template <typename T> Node<T>* merge(Node<T>* first, Node<T>* second){ Node<T> combined, *last_sorted = &combined; while(first != NULL && second != NULL){ if(first->entry <= second->entry){ last_sorted->next = first; last_sorted = first; first = first->next; } else{ last_sorted->next = second; last_sorted = second; second = second->next; } } if(first != NULL){ last_sorted->next = first; // there is a NULL at the end of the first, no need to add } else{ // second != NULL last_sorted->next = second;// there is a NULL at the end of the second, no need to add } /* cout << "check merge\n"; PrintList(combined.next); */ return combined.next; } template<typename T> void MergeSort(Node<T>*& list){ if(list != NULL && list->next != NULL){ // at least two element Node<T>* second_part = divide(list);// divide list into two parts MergeSort(list); MergeSort(second_part); list = merge(list, second_part); // that why use parameter [Node<T>*& list] } return; } template <typename T> void MergeSort(Node<T> *&list, int len) { if(len > 1){ //at least there are two elements Node<T> * second , *end_of_the_first_sublist = list; for(int i = 0; i < (len - 1) / 2; i++){ // discuss for odd and for even end_of_the_first_sublist = end_of_the_first_sublist -> next; } second = end_of_the_first_sublist -> next; end_of_the_first_sublist -> next = NULL; // add an end to the first part int len_of_first_sublist = (len + 1)/ 2; // second part is [len-len_of_first_sublist] MergeSort(list, len_of_first_sublist); MergeSort(second, len-len_of_first_sublist); list = merge(list, second); } return; } #define MAXN 20 int main() { Node<int> *list = new Node<int>(1); for(int i = 2; i <= MAXN; i++){ list = new Node<int>(i, list); } cout << "before sort:\n"; PrintList(list); //MergeSort(list); MergeSort(list, MAXN); cout << "after sorted\n"; PrintList(list); return 0; }
相关文章推荐
- java 接口、抽象类、具体类、内部类、匿名内部类的区别及它们之间的关系
- HTTP协议详解(版本二)
- windbg ida需要symbols
- Eclipse+APKTool动态调试APK
- c 开源代码
- poj 2689
- rz和sz 和他们的参数们
- effective c++ 令operator=返回一个reference to *this
- ICV hash校验值不符的问题
- 工厂模式
- SpringMVC
- poj 1848 树形dp(添加最少的边每点都恰在一个圈中)
- ubuntu下grub启动硬盘iso安装ubuntu
- 再回首【Android 自定义View (二) 进阶】
- Asp.Net alert 方法
- Oracle直接路径加载--append的深度解析
- JSTL标签
- Ajax Post 传输对象或数组的解决方案
- 求两个有序数组的中位数(扩展求第k大元素)
- hdu 5447 Good Numbers(数论)