排序算法 C++模板类
下面的代码是在VS2010中编译过的,运行正确。
////////sort.h///////////////////// #ifndef _SORT_H_ #define _SORT_H_ template<typename T> class CSort{ private: //QuickSort,SelectSort,HeapSort均使用 void swap(T *left, T *right); //the following three functions are for HeapSort() void FilterDown(T x[], const int i, const int n); //the following two functions are for MergeSort() void MSort(T x[], T temp[], int left, int right); void Merge(T x[], T temp[], int leftpos, int right, int rightend); //for QuickSort() T Partition(T x[], const int left, const int right); public: void InsertSort(T x[], const int n); void ShellSort(T x[], const int n); void BinarySort(T x[], const int n); void BublleSort(T x[], const int n); void SelectSort(T x[], const int n); void HeapSort(T x[], const int n); void MergeSort(T x[], int n); void QuickSort(T x[], int left, int right); }; ///////////插入排序//////////////////////////////// //直接插入排序(Insert Sort),时间复杂度为O(n*n),是一种稳定的排序算法 template<typename T> inline void CSort<T>::InsertSort(T x[], const int n) { int i, j; T temp; for (i = 1; i < n; i++) { temp = x[i]; j = i -1; while(j >= 0 && x[j] > temp) { x[j+1] = x[j]; j--; } x[j+1] = temp; } } //希尔排序(Shell Sort),时间复杂度为n的1.2次幂 template<typename T> inline void CSort<T>::ShellSort(T x[], const int n) { int i, j; int dk; T temp; for (dk = n/2; dk > 0; dk /= 2) { for(i = dk; i < n; i++) { temp = x[i]; j = i - dk; while(j >= 0 && x[j] > temp) { x[j+dk] = x[j]; j -= dk; } x[j + dk] = temp; } } } //折半插入排序(Binary Insert Sort),时间复杂度为O(n*n),是一种稳定的排序算法 template<typename T> inline void CSort<T>::BinarySort(T x[], const int n) { for (int i = 1; i < n; i++) { int left = 0, right = i - 1; T temp = x[i]; while(left <= right) { int middle = (left + right)/2; if (temp < x[middle]) { right = middle - 1; } else left = middle + 1; } for (int k = i -1; k >= left; k--) { x[k+1] = x[k]; } x[left] = temp; } } ////////////////交换排序////////////////////////// //冒泡排序(Bubble Sort),如为正序,则时间复杂度为O(n);如果为逆序,则时间复杂度为O(n*n) //因此总的平均时间复杂度为O(n*n),是一种稳定的排序算法 template<typename T> inline void CSort<T>::BublleSort(T x[], const int n) { int i, j; T temp; for (i = 0; i < n; i++) { for (j = 0; j < n-i-1; j++) { if (x[j] > x[j+1]) { temp = x[j]; x[j] = x[j+1]; x[j+1] = temp; } } } } //快速排序(Quick Sort),时间复杂度为n*log2(n),是一种不稳定的排序算法 template<typename T> inline void CSort<T>::swap(T *left, T *right) { T temp = *left; *left = *right; *right = temp; } template<typename T> inline T CSort<T>::Partition(T x[], const int left, const int right) { int pivotpos = left; T temp = x[left]; int i; for (i = left+1; i <= right; i++) { if (x[i] < temp && ++pivotpos != i) { swap(&x[pivotpos], &x[i]); } } swap(&x[left], &x[pivotpos]); return pivotpos; } template<typename T> inline void CSort<T>::QuickSort(T x[], int left, int right) { if (left < right) { int pivotpos = Partition(x, left, right); QuickSort(x, left, pivotpos - 1); QuickSort(x, pivotpos + 1, right); } } /////////////归并排序////////////////////////////////////// //时间复杂度为n*log2(n),是一种稳定的排序算法 template<typename T> inline void CSort<T>::Merge(T x[], T tmp[], int lpos, int rpos, int rightend) { int i; int leftend; int numelements; int tmppos; leftend = rpos - 1; tmppos = lpos; numelements = rightend - lpos + 1; // main loop while ((lpos <= leftend) && (rpos <= rightend)) { if (x[lpos] <= x[rpos]) tmp[tmppos++] = x[lpos++]; else tmp[tmppos++] = x[rpos++]; } while (lpos <= leftend) // copy rest of first half tmp[tmppos++] = x[lpos++]; while (rpos <= rightend) // copy rest of second half tmp[tmppos++] = x[rpos++]; // copy tmp back for (i = 0; i < numelements; ++i, --rightend) x[rightend] = tmp[rightend]; } template<typename T> inline void CSort<T>::MSort(T x[], T tmp[], int left, int right) { int center; if (left < right) { center = (left + right) / 2; MSort(x, tmp, left, center); MSort(x, tmp, center + 1, right); Merge(x, tmp, left, center + 1, right); } } template<typename T> inline void CSort<T>::MergeSort(T x[], int n) { T *tmp; tmp = new (T[n * sizeof(T)]); if (NULL != tmp) { MSort(x, tmp, 0, n - 1); delete tmp; } } /////////////////选择排序////////////////////////// //直接选择排序,时间复杂度为O(n*n),是一种不稳定的排序算法 template<typename T> inline void CSort<T>::SelectSort(T x[], const int n) { int i; for (i = 0; i < n-1; i++) { int k = i; for (int j = i+1; j < n; j++) { if (x[j] < x[k]) { k = j; } } if (k != i) { swap(&x[i], &x[k]); } } } //堆排序(Heap Sort),时间复杂度为n*log2(n),空间复杂度为O(1),是一种不稳定的排序算法 template<typename T> inline void CSort<T>::FilterDown(T x[], const int i, const int n) { int current = i; int child = 2 * i + 1; T temp = x[i]; while(child <= n) { if (child < n && x[child] < x[child + 1]) { child = child + 1; } if (temp >= x[child]) { break; } else { x[current] = x[child]; current = child; child = 2 * child + 1; } } x[current] = temp; } template<typename T> inline void CSort<T>::HeapSort(T x[], const int n) { for (int i = (n - 2)/2; i >= 0; i--) { FilterDown(x, i, n - 1); } for (int i = n - 1; i >= 1; i--) { swap(&x[0], &x[i]); FilterDown(x,0, i-1); } } #endif
//////sort.cpp////////// #include "stdafx.h" #include "sort.h" #include <iostream> using namespace std; int main() { int x[] = {2, 9, 1, 6, 4, 8, 10, 7, 3, 5}; int length = sizeof(x)/sizeof(x[1]); CSort<int>sort; //sort.InsertSort(x,length); //sort.ShellSort(x, length); //sort.BublleSort(x, length); //sort.BinarySort(x, length); sort.MergeSort(x, length); //sort.SelectSort(x, length); //sort.QuickSort(x, 0, length-1); //sort.HeapSort(x, length); for (int i = 0; i < length; i++) { cout << x[i] << " "; } cout << endl; system("PAUSE"); return 0; }