zoukankan      html  css  js  c++  java
  • STL: 排序

    sort

    Arranges the elements in a specified range into a nondescending order or according to an ordering criterion specified by a binary predicate.

    template<class RandomAccessIterator>
       void sort(
          RandomAccessIterator first, 
          RandomAccessIterator last
       );
    template<class RandomAccessIterator, class Predicate>
       void sort(
          RandomAccessIterator first, 
          RandomAccessIterator last, 
          Predicate comp
       );

    注,毫无疑问,STL提供的sort的速度非常快,它是基于改编的quicksort实现的。《编程珠玑》排序一章的实验结果表明是最快的。所以没有特殊需要建议直接使用sort。

    当然如果仅仅需要对少量的部分数据排序,partial_sort可能是一个不错的选择,它使用堆实现,由于不需要保证所有的元素的有序,因而可能更快。

    但是,如果需要稳定排序,那么可以使用stable_sort.

    stable_sort

    Arranges the elements in a specified range into a nondescending order or according to an ordering criterion specified by a binary predicate and preserves the relative ordering of equivalent elements.

     
    template<class BidirectionalIterator> 
       void stable_sort( 
          BidirectionalIterator _First,  
          BidirectionalIterator _Last 
       ); 
    template<class BidirectionalIterator, class BinaryPredicate> 
       void stable_sort( 
          BidirectionalIterator _First,  
          BidirectionalIterator _Last, 
          BinaryPredicate _Comp 
       );

    注,The run-time complexity of stable_sort depends on the amount of memory available, but the best case (given sufficient memory) is O( N log N) and the worst case is O( N ( log N)2 ), where N = _Last – First. Usually, the sort algorithm is significantly faster than stable_sort.

    partial_sort

    Arranges a specified number of the smaller elements in a range into a nondescending order or according to an ordering criterion specified by a binary predicate.

    template<class RandomAccessIterator>
       void partial_sort(
          RandomAccessIterator first, 
          RandomAccessIterator sortEnd,
          RandomAccessIterator last
       );
    template<class RandomAccessIterator, class BinaryPredicate>
       void partial_sort(
          RandomAccessIterator first, 
          RandomAccessIterator sortEnd,
          RandomAccessIterator last
          BinaryPredicate comp
       );

    注,该函数内部是使用堆实现部分排序操作,按理说,对于仅要求少量部分有序的序列使用partial_sort的速度快于完全排序的函数,如:sort。

    partial_sort_copy

    Copies elements from a source range into a destination range where the source elements are ordered by either less than or another specified binary predicate.

    template<class InputIterator, class RandomAccessIterator> 
       RandomAccessIterator partial_sort_copy( 
          InputIterator _First1,  
          InputIterator _Last1, 
          RandomAccessIterator _First2,  
          RandomAccessIterator _Last2 
       ); 
    template<class InputIterator, class RandomAccessIterator, class BinaryPredicate> 
       RandomAccessIterator partial_sort_copy( 
          InputIterator _First1,  
          InputIterator _Last1, 
          RandomAccessIterator _First2,  
          RandomAccessIterator _Last2, 
          BinaryPredicate _Comp 
       );

    nth_element

    Partitions a range of elements, correctly locating the nth element of the sequence in the range so that all the elements in front of it are less than or equal to it and all the elements that follow it in the sequence are greater than or equal to it.

    template<class RandomAccessIterator> 
       void nth_element( 
          RandomAccessIterator _First,  
          RandomAccessIterator _Nth,  
          RandomAccessIterator _Last 
       ); 
    template<class RandomAccessIterator, class BinaryPredicate> 
       void nth_element( 
          RandomAccessIterator _First,  
          RandomAccessIterator _Nth,  
          RandomAccessIterator _Last, 
          BinaryPredicate _Comp 
       );

    注,The nth_element algorithm does not guarantee that elements in the sub-ranges either side of the nth element are sorted. It thus makes fewer guarantees than partial_sort, which orders the elements in the range below some chosen element, and may be used as a faster alternative to partial_sort when the ordering of the lower range is not required. 所以对于并不要求最终结果有序时,可以优先考虑nth_element,要求部分有序可以使用partial_sort,要求完全有序使用sort.

  • 相关阅读:
    js在线压缩网址
    java 排序
    java访问权限(子类继承父类的方法)
    重庆大学第八届编程大赛初赛1、2题目
    重庆大学oj系统——习题
    种花问题(Can Place Flowers)——两朵花不能相邻
    TFIDF计算
    python自定义聚合函数,merge与transform的区别
    AFM模型 pytorch示例代码
    python计算信息增益
  • 原文地址:https://www.cnblogs.com/freewater/p/2954204.html
Copyright © 2011-2022 走看看