0001 template <typename T> void Vector<T>::selectionSort(Rank lo, Rank hi) { 0002 while (lo < --hi) 0003 swap(_elem[getMax(lo, hi)], _elem[hi]); //将[hi]与[lo, hi]中的最大者交换 0004 } //selectionSort 0005 0006 template <typename T> Rank Vector<T>::getMax(Rank lo, Rank hi) { 0007 Rank mx = hi; 0008 while (lo < hi--) //逆向扫描 0009 if (_elem[hi] > _elem[mx]) //且严格比较 0010 mx = hi; //故能在max有多个时保证后者优先,进而保证selectionSort稳定 0011 return mx; 0012 } //getMax