Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/algorithm/inplace merge"

From cppreference.com
< cpp‎ | algorithm
(+type reqs)
(note on allocation)
Line 29: Line 29:
  
 
===Complexity===
 
===Complexity===
 +
Exactly {{math|N-1}} comparisons if enough additional memory is available, otherwise {{math|N&middot;log(N)}} where {{c|1=N = std::distance(first, last)}}.
  
Exctly {{math|N-1}} comparisons if enough additional memory is available, otherwise {{math|N&middot;log(N)}} where {{c|1=N = std::distance(first, last)}}.
+
===Notes===
 +
This function attempts to allocate a temporary buffer, typically by calling {{c|std::get_temporary_buffer}}. If the allocation fails, the less efficient algorithm is chosen.
  
 
===Example===
 
===Example===

Revision as of 05:47, 16 August 2012

 
 
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy, ranges::sort, ...
Execution policies (C++17)
Non-modifying sequence operations
Batch operations
(C++17)
Search operations
(C++11)                (C++11)(C++11)

Modifying sequence operations
Copy operations
(C++11)
(C++11)
Swap operations
Transformation operations
Generation operations
Removing operations
Order-changing operations
(until C++17)(C++11)
(C++20)(C++20)
Sampling operations
(C++17)

Sorting and related operations
Partitioning operations
Sorting operations
Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
inplace_merge
Heap operations
Minimum/maximum operations
(C++11)
(C++17)
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
 

Template:ddcl list begin <tr class="t-dsc-header">

<td>
Defined in header <algorithm>
</td>

<td></td> <td></td> </tr> <tr class="t-dcl ">

<td >
template< class BidirIt >
void inplace_merge( BidirIt first, BidirIt middle, BidirIt last );
</td>

<td > (1) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl ">

<td >
template< class BidirIt, class Compare>
void inplace_merge( BidirIt first, BidirIt middle, BidirIt last, Compare comp );
</td>

<td > (2) </td> <td class="t-dcl-nopad"> </td> </tr> Template:ddcl list end

Merges two consecutive sorted ranges [first, middle) and [middle, last) into one sorted range [first, last). The order of equal elements is guaranteed to be preserved. The first version uses operator< to compare the elements, the second version uses the given comparison function comp.

Contents

Parameters

first - the beginning of the first sorted range
middle - the end of the first sorted range and the beginning of the second
last - the end of the second sorted range
comp - comparison function object (i.e. an object that satisfies the requirements of Compare) which returns true if the first argument is less than the second.

The signature of the comparison function should be equivalent to the following:

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

While the signature does not need to have const&, the function must not modify the objects passed to it and must be able to accept all values of type (possibly const) Type1 and Type2 regardless of value category (thus, Type1& is not allowed, nor is Type1 unless for Type1 a move is equivalent to a copy(since C++11)).
The types Type1 and Type2 must be such that an object of type BidirIt can be dereferenced and then implicitly converted to both of them.

Type requirements
-
BidirIt must meet the requirements of LegacyBidirectionalIterator.

Return value

(none)

Complexity

Exactly N-1 comparisons if enough additional memory is available, otherwise N·log(N) where N = std::distance(first, last).

Notes

This function attempts to allocate a temporary buffer, typically by calling std::get_temporary_buffer. If the allocation fails, the less efficient algorithm is chosen.

Example

The following code is an implementation of merge sort.

#include <vector>
#include <iostream>
#include <algorithm>
 
template<class Iter>
void merge_sort(Iter first, Iter last)
{
    if (last - first > 1) {
        Iter middle = first + (last - first) / 2;
        merge_sort(first, middle);
        merge_sort(middle, last);
        std::inplace_merge(first, middle, last);
    }
}
 
int main()
{
    std::vector<int> v{8, 2, -2, 0, 11, 11, 1, 7, 3};
    merge_sort(v.begin(), v.end());
    for(auto n : v) {
        std::cout << n << ' ';
    }
    std::cout << '\n';
}

Output:

-2 0 1 2 3 7 8 11 11

See also

Template:cpp/algorithm/dcl list mergeTemplate:cpp/algorithm/dcl list sortTemplate:cpp/algorithm/dcl list stable sort