关于快速排序的c++算法(2)

-BASH-4.0.35$ cat quicksort.cc #include<iostream> #include<algorithm> #include<vector> using namespace std; class QuickSort{ private: vector<int> buf; protected: int partition(int p, int r); void Q_Sort(int p, int r); public: QuickSort(int n) { int m; for( int i=0; i< n; i++) { cin>>m; buf.push_back(m); } } void Sort(){ Q_Sort(0,buf.size()-1);} void Q_Show(); }; int QuickSort::partition( int p, int r) { int x=buf.at(r); int i=p-1; for( int j=p;j<=r-1;j++) { if( buf.at(j)<=x) { i++; swap(buf.at(i),buf.at(j)); } } swap(buf.at(i+1),buf.at(r)); return i+1; } void QuickSort::Q_Sort(int p, int r) { if( p< r) { int q=partition(p,r); Q_Sort(p,q-1); Q_Sort(q+1,r); } } void QuickSort::Q_Show() { vector<int>::iterator it; for( it = buf.begin();it!=buf.end();it++) { cout<<*it<<" "; } cout<<endl; } int main() { int m; cin>>m; QuickSort qs(m); qs.Q_Show(); qs.Sort(); qs.Q_Show(); return 0; } -BASH-4.0.35$ Devil<[email protected]> 15:11:20 int QuickSort::partition( int p, int r) { int x=buf.at(r); int i=p-1; for( int j=p;j<=r-1;j++) { if( buf.at(j)<=x) { i++; swap(buf.at(i),buf.at(j)); } } swap(buf.at(i+1),buf.at(r)); return i+1; }

你可能感兴趣的:(关于快速排序的c++算法(2))