原文:https://www.cnblogs.com/wanmeishenghuo/p/9688158.html 参考狄泰软件相关教程
归并示意图:
程序入下:
#ifndef SORT_H #define SORT_H #include "Object.h" namespace DTLib { class Sort : public Object { private: Sort(); Sort(const Sort&); Sort& operator = (const Sort&); template <typename T> static void Swap(T& a, T& b) { T c(a); a = b; b = c; } template < typename T > static void Merge(T src[], T helper[], int begin, int mid, int end, bool min2max=true) { int i = begin; int j = mid + 1; int k = begin; //代表辅助空间起始位置 while( (i <= mid) && (j <= end) ) { if( min2max ? (src[i] < src[j]) : (src[i] > src[j]) ) { helper[k++] = src[i++]; } else { helper[k++] = src[j++]; } } while( i <= mid) { helper[k++] = src[i++]; } while( j <= end ) { helper[k++] = src[j++]; } for(i = begin; i <= end; i++) { src[i] = helper[i]; } } template < typename T > static void Merge(T src[], T helper[], int begin, int end, bool min2max=true) { if( begin < end ) { int mid = (begin + end) / 2; Merge(src, helper, begin, mid, min2max); Merge(src, helper, mid+1, end, min2max); Merge(src, helper, begin, mid, end, min2max); //真正的归并操作 } } public: template < typename T > static void Select(T array[], int len, bool min2max=true) { for(int i = 0; i < len; i++) { int min = i; for(int j = i + 1; j < len; j++) { if( min2max ? (array[min] > array[j]) : (array[min] < array[j]) ) { min = j; } } if( min != i) { Swap(array[i], array[min]); } } } template < typename T > static void Insert(T array[], int len, bool min2max=true) { for(int i=1; i < len; i++) //从1开始,第0个元素没有必要插入操作 { int k = i; T e = array[i]; for(int j=i-1; (j>=0) && (min2max ? (array[j] > e) : (array[j] < e)); j--) { array[j+1] = array[j]; k = j; } if( k != i ) //赋值比“比较操作耗时” { array[k] = e; } } } template < typename T > static void Bubble(T array[], int len, bool min2max=true) { bool exchange = true; for(int i=0; (i<len) && exchange; i++) { exchange = false; for(int j=len-1; j>i; j--) { if(min2max ? (array[j] < array[j-1]) : (array[j] > array[j-1])) { Swap(array[j], array[j-1]); exchange = true; } } } } template < typename T > static void Shell(T array[], int len, bool min2max=true) { int d = len; do { d = d / 3 + 1; //d的减小方式(实践证明这样做效果比较好) for(int i = d; i < len; i+=d) { int k = i; T e = array[i]; for(int j=i-d; (j>=0) && (min2max ? (array[j] > e) : (array[j] < e)); j-=d) { array[j+d] = array[j]; k = j; } if( k != i ) //赋值比“比较操作耗时” { array[k] = e; } } }while( d > 1 ); } template < typename T > static void Merge(T array[], int len, bool min2max=true) { T* helper = new T[len]; if( helper != NULL ) { Merge(array, helper, 0, len - 1, min2max); } delete[] helper; } }; } #endif // SORT_H
注意:归并排序是一种稳定的排序算法
图解:
代码如下:
#ifndef SORT_H #define SORT_H #include "Object.h" namespace DTLib { class Sort : public Object { private: Sort(); Sort(const Sort&); Sort& operator = (const Sort&); template <typename T> static void Swap(T& a, T& b) { T c(a); a = b; b = c; } template < typename T > static void Merge(T src[], T helper[], int begin, int mid, int end, bool min2max=true) { int i = begin; int j = mid + 1; int k = begin; //代表辅助空间起始位置 while( (i <= mid) && (j <= end) ) { if( min2max ? (src[i] < src[j]) : (src[i] > src[j]) ) { helper[k++] = src[i++]; } else { helper[k++] = src[j++]; } } while( i <= mid) { helper[k++] = src[i++]; } while( j <= end ) { helper[k++] = src[j++]; } for(i = begin; i <= end; i++) { src[i] = helper[i]; } } template < typename T > static void Merge(T src[], T helper[], int begin, int end, bool min2max) { if( begin < end ) { int mid = (begin + end) / 2; Merge(src, helper, begin, mid, min2max); Merge(src, helper, mid+1, end, min2max); Merge(src, helper, begin, mid, end, min2max); //真正的归并操作 } } template < typename T > static int Partition(T array[], int begin, int end, bool min2max) { T pv = array[begin]; while( begin < end ) { while( (begin < end) && (min2max ? (array[end] > pv) : (array[end] < pv)) ) { end--; } Swap(array[begin], array[end]); while( (begin < end) && (min2max ? (array[begin] <= pv) : (array[begin] >= pv)) ) { begin++; } Swap(array[begin], array[end]); } array[begin] = pv; //基准就位 return begin; } template < typename T > static void Quick(T array[], int begin, int end, bool min2max) { if( begin < end ) { int pivot = Partition(array, begin, end, min2max); Quick(array, begin, pivot - 1, min2max); Quick(array, pivot + 1, end, min2max); } } public: template < typename T > static void Select(T array[], int len, bool min2max=true) { for(int i = 0; i < len; i++) { int min = i; for(int j = i + 1; j < len; j++) { if( min2max ? (array[min] > array[j]) : (array[min] < array[j]) ) { min = j; } } if( min != i) { Swap(array[i], array[min]); } } } template < typename T > static void Insert(T array[], int len, bool min2max=true) { for(int i=1; i < len; i++) //从1开始,第0个元素没有必要插入操作 { int k = i; T e = array[i]; for(int j=i-1; (j>=0) && (min2max ? (array[j] > e) : (array[j] < e)); j--) { array[j+1] = array[j]; k = j; } if( k != i ) //赋值比“比较操作耗时” { array[k] = e; } } } template < typename T > static void Bubble(T array[], int len, bool min2max=true) { bool exchange = true; for(int i=0; (i<len) && exchange; i++) { exchange = false; for(int j=len-1; j>i; j--) { if(min2max ? (array[j] < array[j-1]) : (array[j] > array[j-1])) { Swap(array[j], array[j-1]); exchange = true; } } } } template < typename T > static void Shell(T array[], int len, bool min2max=true) { int d = len; do { d = d / 3 + 1; //d的减小方式(实践证明这样做效果比较好) for(int i = d; i < len; i+=d) { int k = i; T e = array[i]; for(int j=i-d; (j>=0) && (min2max ? (array[j] > e) : (array[j] < e)); j-=d) { array[j+d] = array[j]; k = j; } if( k != i ) //赋值比“比较操作耗时” { array[k] = e; } } }while( d > 1 ); } template < typename T > static void Merge(T array[], int len, bool min2max=true) { T* helper = new T[len]; if( helper != NULL ) { Merge(array, helper, 0, len - 1, min2max); } delete[] helper; } template < typename T > static void Quick(T array[], int len, bool min2max=true) { Quick(array, 0, len - 1, min2max); } }; } #endif // SORT_H
注意:快速排序是一种不稳定的排序算法
小结: