快速排序
2012-09-12 21:49 neu_zjujunge 阅读(160) 评论(0) 收藏 举报部分源代码
typedef int KeyType; #include "SqTable.h" template<typename D>class QkSort:public SqTable<D> { private: int Partition(int low, int high) { int i=low, j=high; elem[0]=elem[low]; while(low<high) { while(low<high && elem[high].key>=elem[0].key) --high; elem[low]=elem[high]; while(low<high&&elem[low].key<=elem[0].key) ++low; elem[high]=elem[low]; } elem[low]=elem[0]; cout<<endl<<"pivotloc="<<low<<" Partition("<<i<<", "<<j<<")="<<endl; Traverse(Visit); return low; } void QSort(int low, int high) { if(low<high) { int pivotloc=Partition(low, high); QSort(low, pivotloc-1); QSort(pivotloc+1, high); } } public: void QuickSort() { QSort(1, length); } };
示意图

浙公网安备 33010602011771号