//快速排序 #include <iostream> using namespace std; #define N 10240 template< typename T > void sort( T* a, int n ){ if(n<=0) return; if(n==2) { if(a[1] < *a) swap(a[1],*a); return; } swap( *a,a[n>>1] ); T v = *a; T* left = a+1; T* right = a+n-1; while( left<right ){ while( left<right&&*left<v ) ++left; while( *right>v&&right>a ) --right; if( left<right ) swap(*left,*right ); } swap( *a, *right ); sort( a, right-a ); sort( right+1, n-1-(right-a) ); }
int main() { int a[N]; for( int i=0; i<N; i++) a[i] = N-i; time_t t = time(NULL); sort(a,N); cout << "Time : " << time(NULL)-t << endl; for( int i=0; i<10; i++) cout << a[i] << ' ' ; cout << endl; }
//二叉树 #include <iostream> using namespace std;
template< typename T > class BanaryTree{ private: struct Node{ T data; Node* left; Node* right; Node():data(T()),left(NULL),right(NULL){ } Node(const T& t):data(t),left(NULL),right(NULL){ } }; // the end of struct