标题:STL: nth_element为什么结果和sort一样?
只看楼主
hwdwow
Rank: 2
等 级:论坛游民
帖 子:119
专家分:98
注 册:2009-3-21
结帖率:72%
已结贴  问题点数:20 回复次数:5 
STL: nth_element为什么结果和sort一样?
#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
#include <time.h>

using namespace std;

int Random(int low=0, int high=RAND_MAX)
{
    return rand()%(high-low+1)+low;
}


bool exam(int x)
{
    return x>=60;
}
   
int main()
{
    int i;
    vector<int> a;
    vector<int> b;
    srand((unsigned)time(0));
    for (i=0; i<10; i++)
        a.push_back(Random(1,100));
    copy(a.begin(),a.end(),ostream_iterator<int>(cout,","));
    cout<< endl;

    b=a;
    sort(b.begin(), b.end());
    cout<< "sort: ";
    copy(b.begin(),b.end(),ostream_iterator<int>(cout,","));
    cout<< endl;

    b=a;
    sort(b.begin(), b.end(), greater<int>());
    cout<< "sort(greater): ";
    copy(b.begin(),b.end(),ostream_iterator<int>(cout,","));
    cout<< endl;

    b=a;
    nth_element(b.begin(), b.begin()+3, b.end());    // 为什么结果和sort一样?
    cout<< "nth_element: ";
    copy(b.begin(),b.end(),ostream_iterator<int>(cout,","));
    cout<< endl;

    b=a;
    partial_sort(b.begin(), b.begin()+3, b.end());
    cout<< "partial_sort: ";
    copy(b.begin(),b.end(),ostream_iterator<int>(cout,","));
    cout<< endl;

    vector<int> c(3);
    b=a;
    partial_sort_copy(b.begin(), b.end(), c.begin(), c.end());
    cout<< "partial_sort_copy: ";
    copy(c.begin(),c.end(),ostream_iterator<int>(cout,","));
    cout<<"   b: ";
    copy(b.begin(),b.end(),ostream_iterator<int>(cout,","));
    cout<< endl;

    b=a;
    partition(b.begin(), b.end(), exam);
    cout<< "partition: ";
    copy(b.begin(),b.end(),ostream_iterator<int>(cout,","));
    cout<< endl;
   
    return 0;
}
搜索更多相关主题的帖子: 结果 nth sort element STL 
2009-10-08 07:41
pangding
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:北京
等 级:贵宾
威 望:94
帖 子:6784
专家分:16751
注 册:2008-12-20
得分:10 
应该不一样吧。
你运行的结果是什么?发一个上来看看。

下面是我的一次运行结果:
49,85,30,5,28,69,86,77,18,84,
sort: 5,18,28,30,49,69,77,84,85,86,
sort(greater): 86,85,84,77,69,49,30,28,18,5,
nth_element: 28,18,5,30,49,69,86,77,85,84,    // 不一样吧~
partial_sort: 5,18,28,85,49,69,86,77,30,84,
partial_sort_copy: 5,18,28,   b: 49,85,30,5,28,69,86,77,18,84,
partition: 84,85,77,86,69,28,5,30,18,49,


[ 本帖最后由 pangding 于 2009-10-8 10:51 编辑 ]
2009-10-08 10:48
hwdwow
Rank: 2
等 级:论坛游民
帖 子:119
专家分:98
注 册:2009-3-21
得分:0 
4,70,49,62,32,32,83,38,2,72,
sort: 2,4,32,32,38,49,62,70,72,83,
sort(greater): 83,72,70,62,49,38,32,32,4,2,
nth_element: 2,4,32,32,38,49,62,70,72,83,     // 一样啊,郁闷
partial_sort: 2,4,32,70,62,49,83,38,32,72,
partial_sort_copy: 2,4,32,   b: 4,70,49,62,32,32,83,38,2,72,
partition: 72,70,83,62,32,32,49,38,2,4,
list::sort:  2,4,32,32,38,49,62,70,72,83,
Press any key to continue
2009-10-11 09:51
pangding
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:北京
等 级:贵宾
威 望:94
帖 子:6784
专家分:16751
注 册:2008-12-20
得分:0 
多试几次每次都一样吗?如果是那样的话,你那个库用的算法就不太好
2009-10-11 09:54
lyqmath
Rank: 2
等 级:论坛游民
威 望:1
帖 子:9
专家分:18
注 册:2009-5-3
得分:10 
确实一样啊,我运行了5次,发现nth_element和sort按升序排列得到的结果是一样的
2009-10-11 15:33
pangding
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:北京
等 级:贵宾
威 望:94
帖 子:6784
专家分:16751
注 册:2008-12-20
得分:0 
回复 5楼 lyqmath
程序代码:
/*
 *
 * Copyright (c) 1994
 * Hewlett-Packard Company
 *
 * Permission to use, copy, modify, distribute and sell this software
 * and its documentation for any purpose is hereby granted without fee,
 * provided that the above copyright notice appear in all copies and
 * that both that copyright notice and this permission notice appear
 * in supporting documentation.  Hewlett-Packard Company makes no
 * representations about the suitability of this software for any
 * purpose.  It is provided "as is" without express or implied warranty.
 *
 *
 * Copyright (c) 1996
 * Silicon Graphics Computer Systems, Inc.
 *
 * Permission to use, copy, modify, distribute and sell this software
 * and its documentation for any purpose is hereby granted without fee,
 * provided that the above copyright notice appear in all copies and
 * that both that copyright notice and this permission notice appear
 * in supporting documentation.  Silicon Graphics makes no
 * representations about the suitability of this software for any
 * purpose.  It is provided "as is" without express or implied warranty.
 */
……
程序代码:
// nth_element() and its auxiliary functions.  

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);
}

template <class _RandomAccessIter>
inline void nth_element(_RandomAccessIter __first, _RandomAccessIter __nth,
                        _RandomAccessIter __last) {
  __STL_REQUIRES(_RandomAccessIter, _Mutable_RandomAccessIterator);
  __STL_REQUIRES(typename iterator_traits<_RandomAccessIter>::value_type,
                 _LessThanComparable);
  __nth_element(__first, __nth, __last, __VALUE_TYPE(__first));
}
这是一种 nth_element 的实现。从中我们可以看出,它使用的是一种很类似快排的方法,对容器进行排序。但它不全部排序,只要排序可以确定第n大的元素是多少就停止。理论上,在效率方面它比全排一个容器要好很多。 当然由于我只给了部分代码,你可能无法直接使用。比如 __unguarded_partition 和 __insertion_sort 以及一些宏的定义我没给。你要有兴趣可以自己去找相关的实现。或者你可以根据这个算法,自己实现一个来看看效果~
2009-10-14 14:16



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-287827-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021711 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved