【散分】用STL来实现快速排序
大家都熟悉快速排序,这是我用vector容器实现的,本着互相学习的原则,抛砖引玉,大家分享下用其它容器如何来实现同样的功能:
int Partition(vector<int> &lInt, int low, int high) { int Pivot = lInt[low]; while (low < high) { while(low != high && lInt[high] > Pivot) { high--; } if (low != high) { lInt[low++] = lInt[high]; } while (low != high && lInt[low] < Pivot) { low++; } if (low != high) { lInt[high--] = lInt[low]; } } lInt[low] = Pivot; return low; } void QuickSort(vector<int> &lInt, int low, int high) { int pivotops;//划分后基准记录的位置 if (low < high) { pivotops = Partition(lInt, low, high); QuickSort(lInt, low, pivotops-1); QuickSort(lInt, pivotops+1, high); } } int _tmain(int argc, _TCHAR* argv[]) { int myints[] = {75,23,65,42,13}; vector<int> mylist (myints,myints+5); QuickSort(mylist, 0, 4); }