/*************************************************
在区间[first,last)之间的数,第n大的数放在nth位置,
比nth小的数放在之前,大的数放在其后
但是不保证有序
*************************************************/
template <class _RandomAccessIter, class _Tp>
void __nth_element(_RandomAccessIter __first, _RandomAccessIter __nth,
_RandomAccessIter __last, _Tp*) {
while (__last - __first > 3) {
_RandomAccessIter __cut =
__unguarded_partition(__first, __last,
_Tp(__median(*__first,
*(__first + (__last - __first)/2),
*(__last - 1))));
if (__cut <= __nth)
__first = __cut;
else
__last = __cut;
}
__insertion_sort(__first, __last);
}
stl的nth_element
最新推荐文章于 2021-12-16 11:32:46 发布