cpp/algorithm/partial sort:修订间差异

来自cppreference.com
YexuanXiao留言 | 贡献
无编辑摘要
Xmcgcg留言 | 贡献
无编辑摘要
第3行: 第3行:
{{dcl begin}}
{{dcl begin}}
{{dcl header|algorithm}}
{{dcl header|algorithm}}
{{dcl rev multi|num=1|anchor=1|until1=c++20|dcl1=
{{|num=1|=c++20|
template< class RandomIt >
template< class RandomIt >
void partial_sort( RandomIt first, RandomIt middle, RandomIt last );
void partial_sort( RandomIt first, RandomIt middle, RandomIt last );
|dcl2=
template< class RandomIt >
constexpr void partial_sort( RandomIt first, RandomIt middle, RandomIt last );
}}
}}
{{dcl|since=c++17|num=2|1=
{{dcl|since=c++17|
template< class ExecutionPolicy, class RandomIt >
template< class ExecutionPolicy, class RandomIt >
void partial_sort( ExecutionPolicy&& policy,
void partial_sort( ExecutionPolicy&& policy,
                   RandomIt first, RandomIt middle, RandomIt last );
                   RandomIt first, RandomIt middle, RandomIt last );
}}
}}
{{dcl rev multi|num=3|anchor=3|until1=c++20|dcl1=
{{|num=3|=c++20|
template< class RandomIt, class Compare >
template< class RandomIt, class Compare >
void partial_sort( RandomIt first, RandomIt middle, RandomIt last,
void partial_sort( RandomIt first, RandomIt middle, RandomIt last,
                   Compare comp );
                   Compare comp );
|dcl2=
template< class RandomIt, class Compare >
constexpr void partial_sort( RandomIt first, RandomIt middle, RandomIt last,
                            Compare comp );
}}
}}
{{dcl|since=c++17|num=4|1=
{{dcl|since=c++17|
template< class ExecutionPolicy, class RandomIt, class Compare >
template< class ExecutionPolicy, class RandomIt, class Compare >
void partial_sort( ExecutionPolicy&& policy,
void partial_sort( ExecutionPolicy&& policy,
第36行: 第29行:
不保证保持相等元素间的顺序。未指定范围 {{range|middle|last}} 中剩余元素的顺序。
不保证保持相等元素间的顺序。未指定范围 {{range|middle|last}} 中剩余元素的顺序。


@1@ {{c|operator<}} 比较元素
@1@ {{c|operator<}} 。
@3@ 用给定的二元比较函数 {{c|comp}} 比较元素
 
@2,4@ 同 {{v|1,3}},但按照 {{c|policy}} 执行。{{cpp/algorithm/parallel overload precondition|plural=yes}}
@3@ {{c|comp}} 。
 
@2,4@ 同 {{v|1,3}},但按照 {{c|policy}} 执行。
{{cpp/algorithm/parallel overload precondition|plural=yes
 
}}


===参数===
===参数===
{{par begin}}
{{par begin}}
{{par | first, last |定义范围的随机访问迭代器}}
{{par|first, last|定义范围的随机访问迭代器}}
{{par | middle |定义要排序的范围的末尾后一位置的随机访问迭代器}}
{{par|middle|定义要排序的范围的末尾后一位置的随机访问迭代器}}
{{par exec pol}}
{{par exec pol}}
{{par cmp ord | comp | p1=RandomIt}}
{{par cmp ord|comp|p1=RandomIt}}
{{par hreq}}
{{par hreq}}
{{par req named | RandomIt | RandomAccessIterator| ValueSwappable}}  
{{par req named|RandomIt|RandomAccessIterator}}
{{par req named deref | RandomIt | MoveConstructible| MoveAssignable}}  
{{par req named||}}
{{par end}}
{{par end}}
===返回值===
(无)


===复杂度===
===复杂度===
约应用 {{math|(last-first)log(middle-first)}} 次 {{c|cmp}}。
{{|(last - first
)
log()}} 次{{c|}}。


===异常===
===异常===
{{cpp/algorithm/parallel_exceptions_reporting_behavior|singular=no}}
{{cpp/algorithm/|singular=no}}
 
===注解===
====算法====
典型采用''堆选取''{{sep}}算法来选择最小元素,并采用''堆排序''{{sep}}算法以升序对从堆中选取的元素进行排序。
 
使用堆来进行元素的选取(见 {{enwiki|Heap (data structure)#Applications|堆}})。例如,当以 {{c|operator<}} 为比较函数时,使用''最大堆''{{sep}}来选取 {{c|middle − first}} 个最小元素。
 
选择完成后用{{enwiki|Heapsort|堆排序}}对所选取的 {{range|first|middle}} 个元素进行排序(见 {{lc|std::sort_heap}})。
 
====预期用途====
{{c|std::partial_sort}} 算法预期被用于''少量常数个数的'' {{range|first|middle}} 选取元素。


===可能的实现===
===可能的实现===
第76行: 第73行:
|title1=partial_sort (1)|ver1=1|1=
|title1=partial_sort (1)|ver1=1|1=
template<typename RandomIt>
template<typename RandomIt>
// C++20 起为 constexpr
//C++20 起
void partial_sort(RandomIt first, RandomIt middle, RandomIt last)
void partial_sort(RandomIt first, RandomIt middle, RandomIt last)
{
{
第86行: 第83行:
{
{
     template<typename RandomIt, typename Compare>
     template<typename RandomIt, typename Compare>
     // constexpr
     //
     void sift_down(RandomIt first, RandomIt last, const Compare& comp)
     void sift_down(RandomIt first, RandomIt last, const Compare& comp)
     {
     {
         // sift down element at 'first'
         //
         const auto length = static_cast<size_t>(last - first);
         const auto length = static_cast<size_t>(last - first);
         std::size_t current = 0;
         std::size_t current = 0;
         std::size_t next = 2;
         std::size_t next = 2;
第107行: 第104行:
             std::iter_swap(first + current, first + next);
             std::iter_swap(first + current, first + next);
     }
     }
 
     template<typename RandomIt, typename Compare>
     template<typename RandomIt, typename Compare>
     // constexpr
     //
     void heap_select(RandomIt first, RandomIt middle, RandomIt last, const Compare& comp)
     void heap_select(RandomIt first, RandomIt middle, RandomIt last, const Compare& comp)
     {
     {
第125行: 第122行:


template<typename RandomIt, typename Compare>
template<typename RandomIt, typename Compare>
// C++20 起为 constexpr
//C++20 起
void partial_sort(RandomIt first, RandomIt middle, RandomIt last, Compare comp)
void partial_sort(RandomIt first, RandomIt middle, RandomIt last, Compare comp)
{
{
第132行: 第129行:
}
}
}}
}}


===示例===
===示例===
第141行: 第149行:
#include <iostream>
#include <iostream>


void print(auto const& s, int middle)
void print(auto& s, int middle)
{
{
     for (int a : s)
     for (int a : s)
第185行: 第193行:
         ^----------
         ^----------
}}
}}


===参阅===
===参阅===

2024年4月2日 (二) 07:28的版本

 
 
算法库
受约束算法及范围上的算法 (C++20)
包含算法例如 ranges::copy, ranges::sort, ...
执行策略 (C++17)
排序和相关操作
划分操作
排序操作
二分搜索操作(在已划分范围上)
集合操作(在有序范围上)
归并操作(在有序范围上)
堆操作
最小/最大操作
(C++11)
(C++17)
字典序比较操作
排列操作
C 库

数值运算
(C++11)                       
在未初始化内存上的操作
 
在标头 <algorithm> 定义
template< class RandomIt >
void partial_sort( RandomIt first, RandomIt middle, RandomIt last );
(1) (C++20 起为 constexpr)
template< class ExecutionPolicy, class RandomIt >
void partial_sort( ExecutionPolicy&& policy,
                   RandomIt first, RandomIt middle, RandomIt last );
(2) (C++17 起)
template< class RandomIt, class Compare >
void partial_sort( RandomIt first, RandomIt middle, RandomIt last,
                   Compare comp );
(3) (C++20 起为 constexpr)
template< class ExecutionPolicy, class RandomIt, class Compare >
void partial_sort( ExecutionPolicy&& policy,
                   RandomIt first, RandomIt middle, RandomIt last,
                   Compare comp );
(4) (C++17 起)

重排元素,使得范围 [firstmiddle) 含有范围 [firstlast) 中已排序的 middle - first 个最小元素。

不保证保持相等元素间的顺序。未指定范围 [middlelast) 中剩余元素的顺序。

1)operator<(C++20 前)std::less{}(C++20 起) 进行排序
3)comp 进行排序。
2,4)(1,3),但按照 policy 执行。
这些重载只有在满足以下所有条件时才会参与重载决议:

std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>true

(C++20 前)

std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>>true

(C++20 起)

如果满足以下任意条件,那么行为未定义:

(C++11 前)
(C++11 起)

参数

first, last - 定义范围的随机访问迭代器
middle - 定义要排序的范围的末尾后一位置的随机访问迭代器
policy - 所用的执行策略
comp - 比较函数对象(即满足比较 (Compare) 概念的对象),在第一参数小于(即 序于)第二参数时返回 ​true

比较函数的签名应等价于如下:

bool cmp(const Type1 &a, const Type2 &b);

虽然签名不必有 const&,函数也不能修改传递给它的对象,而且必须接受(可为 const 的)类型 Type1Type2 的值,无关乎值类别(从而不允许 Type1& ,也不允许 Type1,除非 Type1 的移动等价于复制(C++11 起))。
类型 Type1Type2 必须使得 RandomIt 类型的对象能在解引用后隐式转换到这两个类型。 ​

类型要求
-
RandomIt 必须满足老式随机访问迭代器 (LegacyRandomAccessIterator)
-
Compare 必须满足比较 (Compare)

复杂度

给定 Mmiddle - firstNlast - first

1,2) 应用大约 N·log(M)operator<(C++20 前)std::less{}(C++20 起) 进行比较。
3,4) 应用大约 N·log(M) 次比较函数 comp

异常

拥有名为 ExecutionPolicy 的模板形参的重载按下列方式报告错误:

  • 如果作为算法一部分调用的函数的执行抛出异常,且 ExecutionPolicy标准策略之一,那么调用 std::terminate。对于任何其他 ExecutionPolicy,行为由实现定义。
  • 如果算法无法分配内存,那么抛出 std::bad_alloc

可能的实现

参阅 libstdc++libc++ 中的实现。

partial_sort (1)
template<typename RandomIt>
constexpr //< C++20 起
void partial_sort(RandomIt first, RandomIt middle, RandomIt last)
{
    typedef typename std::iterator_traits<RandomIt>::value_type VT;
    std::partial_sort(first, middle, last, std::less<VT>());
}
partial_sort (3)
namespace impl
{
    template<typename RandomIt, typename Compare>
    constexpr //< C++20 起
    void sift_down(RandomIt first, RandomIt last, const Compare& comp)
    {
        // 筛出 “first” 位置的元素
        const auto length = static_cast<std::size_t>(last - first);
        std::size_t current = 0;
        std::size_t next = 2;
        while (next < length)
        {
            if (comp(*(first + next), *(first + (next - 1))))
                --next;
            if (!comp(*(first + current), *(first + next)))
                return;
            std::iter_swap(first + current, first + next);
            current = next;
            next = 2 * current + 2;
        }
        --next;
        if (next < length && comp(*(first + current), *(first + next)))
            std::iter_swap(first + current, first + next);
    }
    
    template<typename RandomIt, typename Compare>
    constexpr //< C++20 起
    void heap_select(RandomIt first, RandomIt middle, RandomIt last, const Compare& comp)
    {
        std::make_heap(first, middle, comp);
        for (auto i = middle; i != last; ++i)
        {
            if (comp(*i, *first))
            {
                std::iter_swap(first, i);
                sift_down(first, middle, comp);
            }
        }
    }
} // namespace impl

template<typename RandomIt, typename Compare>
constexpr //< C++20 起
void partial_sort(RandomIt first, RandomIt middle, RandomIt last, Compare comp)
{
    impl::heap_select(first, middle, last, comp);
    std::sort_heap(first, middle, comp);
}

注解

算法

典型采用堆选取 算法来选择最小元素,并采用堆排序 算法以升序对从堆中选取的元素进行排序。

使用堆来进行元素的选取(见 )。例如,当以 operator< 为比较函数时,使用最大堆 来选取 middle first 个最小元素。

选择完成后用堆排序对所选取的 [firstmiddle) 个元素进行排序(见 std::sort_heap)。

预期用途

std::partial_sort 算法预期被用于少量常数个数的 [firstmiddle) 选取元素。

示例

#include <algorithm>
#include <array>
#include <functional>
#include <iostream>

void print(const auto& s, int middle)
{
    for (int a : s)
        std::cout << a << ' ';
    std::cout << '\n';
    if (middle > 0)
    {
        while (middle-- > 0)
            std::cout << "--";
        std::cout << '^';
    }
    else if (middle < 0)
    {
        for (auto i = s.size() + middle; --i; std::cout << "  ")
        {}
        
        for (std::cout << '^'; middle++ < 0; std::cout << "--")
        {}
    }
    std::cout << '\n';
};

int main()
{
    std::array<int, 10> s{5, 7, 4, 2, 8, 6, 1, 9, 0, 3};
    print(s, 0);
    std::partial_sort(s.begin(), s.begin() + 3, s.end());
    print(s, 3);
    std::partial_sort(s.rbegin(), s.rbegin() + 4, s.rend());
    print(s, -4);
    std::partial_sort(s.rbegin(), s.rbegin() + 5, s.rend(), std::greater{});
    print(s, -5);
}

可能的输出:

5 7 4 2 8 6 1 9 0 3

0 1 2 7 8 6 5 9 4 3
------^
4 5 6 7 8 9 3 2 1 0
          ^--------
4 3 2 1 0 5 6 7 8 9
        ^----------

缺陷报告

下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。

缺陷报告 应用于 出版时的行为 正确行为
P0896R4 C++98 [firstmiddle)[middlelast) 不需要有效 其中之一无效时行为未定义

参阅

将给定范围部分排序,确保其按给定元素划分
(函数模板) [编辑]
复制范围中元素并部分排序
(函数模板) [编辑]
将范围中元素排序,同时保持相等元之间的顺序
(函数模板) [编辑]
将范围按升序排序
(函数模板) [编辑]
将范围中前 N 个元素排序
(算法函数对象) [编辑]