您的位置:首页 > 其它

归并排序

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: