#include <iostream> #include <ctime> #include <iterator> #include <vector> #include <algorithm> #include <string> using namespace std; void bubble(vector<int> &a) //冒泡排序 { for(size_t i = 0; i < a.size() - 1; ++i) { for(size_t j = i + 1; j < a.size(); ++j) { if(a[j] < a[i]) { swap(a[i], a[j]); } } } } void insert(vector<int> &a) //插入排序 { size_t j; for(size_t i = 1; i < a.size(); ++i) { int tmp = a[i]; for(j = i; j > 0 && tmp < a[j - 1]; --j) a[j] = a[j - 1]; a[j] = tmp; } } void shellsort(vector<int> &a)//希尔排序 { for(size_t gap = a.size() / 2; gap > 0; gap /= 2) for(size_t i = gap; i < a.size(); ++i) { int tmp = a[i]; size_t j = i; for(; j >= gap && tmp < a[j - gap]; j-=gap) a[j] = a[j - gap]; a[j] = tmp; } } int mypartition(vector<int> &a, int p, int r); void quicksort(vector<int> &a, int i, int r)//算法导论的快速排序 { if(i < r) { int p = mypartition(a, i, r); quicksort(a, i, p - 1); quicksort(a, p + 1, r); } } int mypartition(vector<int> &a, int p, int r) { int x = a[r]; int i = p - 1; for(int j = p; j < r - 1; ++j) { if(a[j] <= x) { ++i; swap(a[j], a[i]); } } swap(a[i + 1], a[r]); return i + 1; } template<class T> void percDown(vector<T> &a, int i, int n); template<class T> void headpsort(vector<T> &a)//堆排序 { for(int i = a.size() / 2; i >= 0; --i) percDown(a, i, a.size()); for(int j = a.size() - 1; j > 0; --j) { swap(a[0], a[j]); percDown(a, 0, j); } } inline int leftChild(int i) { return 2 * i + 1; } template<class T> void percDown(vector<T> &a, int i, int n) { int child; T tmp; for(tmp = a[i]; leftChild(i) < n; i = child) { child = leftChild(i); if(child != n - 1 && a[child] < a[child + 1]) ++child; if(tmp < a[child]) a[i] = a[child]; else break; } a[i] = tmp; } template<class T> void mergeSort(vector<T> &a, vector<T> &theArray, int left, int right); template<class T> void mergeSort(vector<T> &a)//归并排序 { vector<T> theArray(a.size()); mergeSort(a, theArray, 0, a.size() - 1); } template<class T> void mergeSort(vector<T> &a, vector<T> &theArray, int left, int right) { if(left < right) { int center = (left + right) / 2; mergeSort(a, theArray, left, center); mergeSort(a, theArray, center + 1, right); mymerge(a, theArray, left, center + 1, right); } } template<class T> void mymerge(vector<T> &a, vector<T> &theArray, int leftPos, int rightPos, int rightEnd) { int leftEnd = rightPos - 1; int tmpPos = leftPos; int numElements = rightEnd - leftPos + 1; while(leftPos <= leftEnd && rightPos <= rightEnd) { if(a[leftPos] <= a[rightPos]) theArray[tmpPos++] = a[leftPos++]; else theArray[tmpPos++] = a[rightPos++]; } while(leftPos <= leftEnd) theArray[tmpPos++] = a[leftPos++]; while(rightPos <= rightEnd) theArray[tmpPos++] = a[rightPos++]; for(int i = 0; i < numElements; ++i, --rightEnd) a[rightEnd] = theArray[rightEnd]; } template<class T> void insertionSort(vector<T> &a, int left, int right)//插入排序 { int j; for(int i = left; i <= right; ++i) { int tmp = a[i]; for(j = i; j > 0 && tmp < a[j - 1]; --j) a[j] = a[j - 1]; a[j] = tmp; } } template<class T> void my_quicksort(vector<T> &a, int left, int right);//数据结构与算法分析的快速排序 template<class T> void my_quicksort(vector<T> &a) { my_quicksort(a, 0, a.size() - 1); } template<class T> const T& median3(vector<T> &a, int left, int right) { int center = (left + right) / 2; if(a[center] < a[left]) swap(a[center], a[left]); if(a[right] < a[left]) swap(a[right], a[left]); if(a[right] < a[center]) swap(a[center], a[right]); swap(a[center], a[right - 1]); return a[right - 1]; } template<class T> void my_quicksort(vector<T> &a, int left, int right) { if(left + 10 <= right) { T pivot = median3(a, left, right); int i = left, j = right - 1; for(;;) { while(++i,a[i] < pivot){} while(--j,pivot < a[j]){} if(i < j) swap(a[i], a[j]); else break; } swap(a[i], a[right - 1]); my_quicksort(a, left, i - 1); my_quicksort(a, i + 1, right); } else { insertionSort(a, left, right); } } template<class T> class Pointer { public: Pointer(T *rhs = NULL) : pointee(rhs){} bool operator < (const Pointer& rhs) const { return *pointee < *rhs.pointee; } T* comp() const { return pointee; } T operator * () const { return *pointee; } private: T *pointee; }; template<class T> bool operator != (const Pointer<T> &lhs, T* rhs) { return lhs.comp() != rhs; } template<class T> int operator - (const Pointer<T> &lhs, T* rhs) { return lhs.comp() - rhs; } template<class T> void largeObjectSort(vector<T> &a)//通过指针排序 { vector<Pointer<T> > p(a.size()); size_t i, j; int nextj; for(i = 0; i < a.size(); ++i) { p[i] = &a[i]; } my_quicksort(a); for(i = 0; i < a.size(); ++i) if(p[i] != &a[i]) { T tmp = a[i]; for(j = i; p[j] != &a[i]; j = nextj) { nextj = p[j] - &a[0]; a[i] = *p[j]; p[j] = &a[j]; } a[j] = tmp; p[j] = &a[j]; } } int main() { srand(time(NULL)); vector<int> a(500000); for(size_t i = 0; i < a.size(); ++i) a[i] = rand() % 1000; //largeObjectSort(a); // copy(a.begin(), a.end(), ostream_iterator<int>(cout, " ")); //cout << endl; //make_heap(a.begin(), a.end()); //sort_heap(a.begin(), a.end()); //sort(a.begin(), a.end()); //sort(a.begin(), a.end(), less<int>()); //stable_sort(a.begin(), a.end()); //quicksort(a, 0 , a.size()); //bubble(a); //insert(a); //shellsort(a); //headpsort(a); //mergeSort(a); //my_quicksort(a); //copy(a.begin(), a.end(), ostream_iterator<int>(cout, " ")); return 0; }