您的位置:首页 > 编程语言 > Go语言

STL 源码剖析 算法 stl_algo.h -- nth_element

2014-07-20 18:25 429 查看
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie

nth_element

------------------------------------------------------------------------------

描述:重新排序,使得[nth,last)内没有任何一个元素小于[first,nth)内的元素,

但对于[first,nth)和[nth,last)两个子区间内的元素次序则无任何保证。

思路:

1.以 median-of-3-partition 将整个序列分割为更小的左、右子序列

2.如果 nth 迭代器落于左序列,就再对左子序列进行分割,否则就再对右子序列进行分割

3.直到分割后的子序列长大于3,对最后这个待分割的子序列做 Insertion Sort



复杂度:O(n)

源码:

template <class RandomAccessIterator>
inline void nth_element(RandomAccessIterator first, RandomAccessIterator nth,
RandomAccessIterator last) {
__nth_element(first, nth, last, value_type(first));
}

template <class RandomAccessIterator, class T>
void __nth_element(RandomAccessIterator first, RandomAccessIterator nth,
RandomAccessIterator last, T*) {
while (last - first > 3) {
//采用 median-of-3-partition 。参数:(first,last,pivot)
//返回一个迭代器,指向分割后的右段第一个元素
RandomAccessIterator cut = __unguarded_partition
(first, last, T(__median(*first, *(first + (last - first)/2),
*(last - 1))));
if (cut <= nth) //如果  nth 落于右段,再对右段实施分割
first = cut;
else  //如果 nth 落于左段,对左段实施分割
last = cut;
}
__insertion_sort(first, last); //对分割后的子序列做 Insertion Sort
}

template <class RandomAccessIterator, class T>
RandomAccessIterator __unguarded_partition(RandomAccessIterator first,
RandomAccessIterator last,
T pivot) {
while (true) {
while (*first < pivot) ++first;
--last;
while (pivot < *last) --last;
if (!(first < last)) return first;
iter_swap(first, last);
++first;
}
}


示例:
int A[] = {7, 2, 6, 11, 9, 3, 12, 10, 8, 4, 1, 5};
const int N = sizeof(A) / sizeof(int);

nth_element(A, A + 6, A + N);
copy(A, A + N, ostream_iterator<int>(cout, " "));
// The printed result is "5 2 6 1 4 3 7 8 9 10 11 12".
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: