以下两种方法之间是否存在显着差异?方式1使用sort
或partial_sort
,具体取决于向量的大小,而方式2始终使用partial_sort
。我发现方式2更具吸引力,因为我的谓词比示例中的更复杂,所以我不想重复它。但是我想知道partial_sort
是否比sort
表现更差,因为它并不是用来对整个范围进行排序,这就是我倾向于使用方式1的原因。
int main()
{
std::vector<double> vec;
vec.push_back(1.0);
vec.push_back(3.0);
vec.push_back(2.0);
vec.push_back(5.0);
vec.push_back(4.0);
vec.push_back(9.0);
const size_t numBest = 3;
const size_t numTotal= vec.size();
#if WAY1
if (numTotal < numBest)
{
std::sort(vec.begin(), vec.end(), std::not2(std::less<double>()));
}
else
{
std::partial_sort(vec.begin(), vec.begin() + numBest, vec.end(), std::not2(std::less<double>()));
vec.resize(numBest);
}
#elif WAY2
{
const size_t numMiddle = numTotal < numBest ? numTotal : numBest;
std::partial_sort(vec.begin(), vec.begin() + numMiddle, vec.end(), std::not2(std::less<double>()));
vec.resize(numMiddle);
}
#endif
// now vec contains the largest numBest results.
return 0;
}
有些测试表明,如果必须对整个范围进行排序,则partial_sort明显更差(我的用例中的因子为4)。这表明方式1是首选。似乎partial_sort仅用于对整个范围的一小部分进行排序。我在Visual Studio 2010中测试过。
答案 0 :(得分:5)
根据sgi doc,partial_sort
使用 heapsort ,sort
使用 introsort :
partial_sort(first,last,last)具有排序整个范围[first,last]的效果,就像sort(first,last)一样。但是它们使用不同的算法:sort使用introsort算法(quicksort的变体),partial_sort使用heapsort。参见Knuth的5.2.3节(D.E.Knuth,计算机程序设计的艺术。第3卷:排序和搜索.Addison-Wesley,1975。)和J.W.J.Williams(CACM 7,347,1964)。 heapsort和introsort都具有N log(N)阶的复杂度,但是introsort通常会快2到5倍。
因此,它是正常的partial_sort
比sort
慢4倍。
我检查了我的VS2017库,找到了partial_sort
和sort
的实现。它与SGI类似。
template<class _RanIt,
class _Pr> inline
void _Partial_sort_unchecked(_RanIt _First, _RanIt _Mid, _RanIt _Last,
_Pr& _Pred)
{ // order [_First, _Last) up to _Mid, using _Pred
if (_First == _Mid)
return; // nothing to do, avoid violating _Pop_heap_hole_unchecked preconditions
_Make_heap_unchecked(_First, _Mid, _Pred);
for (_RanIt _Next = _Mid; _Next < _Last; ++_Next)
if (_DEBUG_LT_PRED(_Pred, *_Next, *_First))
{ // replace top with new largest
_Iter_value_t<_RanIt> _Val = _STD move(*_Next);
_Pop_heap_hole_unchecked(_First, _Mid, _Next, _STD move(_Val), _Pred);
}
_Sort_heap_unchecked(_First, _Mid, _Pred);
}
template<class _RanIt,
class _Diff,
class _Pr> inline
void _Sort_unchecked1(_RanIt _First, _RanIt _Last, _Diff _Ideal, _Pr& _Pred)
{ // order [_First, _Last), using _Pred
_Diff _Count;
while (_ISORT_MAX < (_Count = _Last - _First) && 0 < _Ideal)
{ // divide and conquer by quicksort
pair<_RanIt, _RanIt> _Mid =
_Partition_by_median_guess_unchecked(_First, _Last, _Pred);
_Ideal /= 2, _Ideal += _Ideal / 2; // allow 1.5 log2(N) divisions
if (_Mid.first - _First < _Last - _Mid.second)
{ // loop on second half
_Sort_unchecked1(_First, _Mid.first, _Ideal, _Pred);
_First = _Mid.second;
}
else
{ // loop on first half
_Sort_unchecked1(_Mid.second, _Last, _Ideal, _Pred);
_Last = _Mid.first;
}
}
if (_ISORT_MAX < _Count)
{ // heap sort if too many divisions
_Make_heap_unchecked(_First, _Last, _Pred);
_Sort_heap_unchecked(_First, _Last, _Pred);
}
else if (2 <= _Count)
_Insertion_sort_unchecked(_First, _Last, _Pred); // small
}
答案 1 :(得分:1)
除了复杂性保证之外,没有什么要求以某种方式实现partial_sort
25.4.1.3 partial_sort [partial.sort]
template void partial_sort(RandomAccessIterator first, RandomAccessIterator中间,RandomAccessIterator最后); 模板 void partial_sort(RandomAccessIterator first,RandomAccessIterator middle, RandomAccessIterator last,Compare comp);
1效果:将第一个中间 - 第一个排序元素从[first,last]范围放入范围[first,middle]。范围[中间,最后]中的其余元素以未指定的顺序放置。
2要求: RandomAccessIterator应满足ValueSwappable的要求 (17.6.3.2)。 *首先的类型应满足要求 MoveConstructible(表20)和MoveAssignable(表22)。
3复杂性:需要大约(最后 - 第一)*日志(中间 - 第一)比较
另一种实现可能是
std::nth_element - average linear time
followed by
std::sort - on the reduced range begin()-nth (n log n)