with algorithms that do more work tend to use fewer resources listed before those that requir more:
I. partition sort
bool hasAcceptableQaulity(const Widfet& w) {
// return whether w has a quality rating of 2 or better;
}
// move all widgets satisfying hasAcceptableQaulity to the front of widgets,
// and return an iterator to the first widget that isn't satisfactory
vector<Widget>::iterator goodEnd = partition(widgets.begin(), widgets.end(),
hasAcceptableQaulity);
II. stable_partition
III. nth_element
其功能是对区间 [_First, _Last) 的元素进行重排,其中位于位置 _Nth 的元素与整个区间排序后位于位置 _Nth 的元素相同,并且满足在位置 _Nth 之前的所有元素都“不大于”它和位置 _Nth 之后的所有元素都“不小于”它,而且并不保证 _Nth 的前后两个区间的所有元素保持有序。
template<class _RanIt, class _Pr>
void nth_element(_RanIt _First, _RanIt _Nth, _RanIt _Last, _Pr _Pred);
bool qualityCompare(const Widget& lhs, const Widget& rhs) {
// return lh's quality is greater than rh's quality
}
// put the best 20 elements at the front of widgets, but do not worry about t
// their order
nth_element(widgets.begin(), widgets.begin() + 19, widgets.end(),
qualityCompare);
IV. partial_sort
bool qualityCompare(const Widget& lhs, const Widget& rhs) {
// return lh's quality is greater than rh's quality
}
// put the best 20 elements (in order) at the front of widgets
partital_sort(widgets.begin(), widgets.begin() + 20, widgets.end(),
qualityCompare);
V. sort
VI. stable_sort