ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
MergeSort.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*---------------------------- File MergeSort.h ----------------------------*/
3/*--------------------------------------------------------------------------*/
47/*--------------------------------------------------------------------------*/
48/*----------------------------- DEFINITIONS --------------------------------*/
49/*--------------------------------------------------------------------------*/
50
51#ifndef __ADS_MergeSort
52 #define __ADS_MergeSort
53/* self-identification: #endif constant */
54
55/*--------------------------------------------------------------------------*/
56/*------------------------------ INCLUDES ----------------------------------*/
57/*--------------------------------------------------------------------------*/
58
59#include "ADS/Contract.h"
60#include "ADS/sequence/List.h"
62#include "ADS/sort/Sorted.h"
63
64#include <algorithm>
65#include <cstddef>
66#include <functional>
67#include <utility>
68#include <vector>
69
70/*--------------------------------------------------------------------------*/
71/*--------------------------- NAMESPACE ------------------------------------*/
72/*--------------------------------------------------------------------------*/
73
74namespace ADS
75{
76
108template< RankStore Store ,
109 class T = std::ranges::range_value_t< std::remove_cvref_t< Store > > ,
110 class C = std::less<> >
111void mergeParts( Store && A , std::vector< T > & B , std::size_t start ,
112 std::size_t mid , std::size_t end , C less = C() ,
113 long long * inv = nullptr );
114
116template< class T , class C = std::less<> >
117void mergeParts( std::vector< T > & A , std::vector< T > & B ,
118 std::size_t start ,
119 std::size_t mid , std::size_t end , C less = C() ,
120 long long * inv = nullptr );
121
151template< RankStore Store ,
152 class T = std::ranges::range_value_t< std::remove_cvref_t< Store > > ,
153 class C = std::less<> >
154void mergeSortRec( Store && A , std::vector< T > & B , std::size_t start ,
155 std::size_t end , std::size_t cutoff , C less = C() ,
156 long long * inv = nullptr );
157
159template< class T , class C = std::less<> >
160void mergeSortRec( std::vector< T > & A , std::vector< T > & B ,
161 std::size_t start , std::size_t end , std::size_t cutoff ,
162 C less = C() , long long * inv = nullptr );
163
174template< RankStore Store ,
175 class T = std::ranges::range_value_t< std::remove_cvref_t< Store > > ,
176 class C = std::less<> >
177void mergeSort( Store && A , std::size_t cutoff = 1 , C less = C() );
178
180template< class T , class C = std::less<> >
181void mergeSort( std::vector< T > & A , std::size_t cutoff = 1 , C less = C() );
182
201template< RankStore Store ,
202 class T = std::ranges::range_value_t< std::remove_cvref_t< Store > > ,
203 class C = std::less<> >
204void mergeSortBottomUp( Store && A , C less = C() );
205
207template< class T , class C = std::less<> >
208void mergeSortBottomUp( std::vector< T > & A , C less = C() );
209
227template< RankStore Store ,
228 class T = std::ranges::range_value_t< std::remove_cvref_t< Store > > ,
229 class C = std::less<> >
230long long countInversions( Store && A , C less = C() );
231
233template< class T , class C = std::less<> >
234long long countInversions( std::vector< T > & A , C less = C() );
235
260template< class T , class C = std::less<> >
261void mergeSort( List< T > & L , C less = C() );
262
263/*--------------------------------------------------------------------------*/
264/*------------------------- THE SORTS OF A RANGE ---------------------------*/
265/*--------------------------------------------------------------------------*/
266
271template< SortRange R , class... P >
272 requires requires( std::vector< std::ranges::range_value_t< R > > & v ,
273 P &&... p )
274{ mergeSort( v , std::forward< P >( p )... ); }
275decltype( auto ) mergeSort( R && A , P &&... p )
276{
277 const auto f = [ & ]( auto & v ) -> decltype( auto ) {
278 return( mergeSort( v , std::forward< P >( p )... ) );
279 };
280 return( detail::sortRange( A , f , f ) );
281 }
282
287template< SortRange R , class... P >
288 requires requires( std::vector< std::ranges::range_value_t< R > > & v ,
289 P &&... p )
290{
291 mergeSortBottomUp( v , std::forward< P >( p )... );
292 }
293decltype( auto ) mergeSortBottomUp( R && A , P &&... p )
294{
295 const auto f = [ & ]( auto & v ) -> decltype( auto ) {
296 return( mergeSortBottomUp( v , std::forward< P >( p )... ) );
297 };
298 return( detail::sortRange( A , f , f ) );
299 }
300
305template< SortRange R , class... P >
306 requires requires( std::vector< std::ranges::range_value_t< R > > & v ,
307 P &&... p )
308{
309 countInversions( v , std::forward< P >( p )... );
310 }
311decltype( auto ) countInversions( R && A , P &&... p )
312{
313 const auto f = [ & ]( auto & v ) -> decltype( auto ) {
314 return( countInversions( v , std::forward< P >( p )... ) );
315 };
316 return( detail::sortRange( A , f , f ) );
317 }
318
319/*--------------------------------------------------------------------------*/
320
321} // end( namespace ADS )
322
323/*--------------------------------------------------------------------------*/
324/*-------------------------- IMPLEMENTATION --------------------------------*/
325/*--------------------------------------------------------------------------*/
326
327#include "ADS/sort/impl/MergeSort.tpp"
328
329/*--------------------------------------------------------------------------*/
330
331#endif /* MergeSort.h included */
332
333/*--------------------------------------------------------------------------*/
334/*-------------------------- End File MergeSort.h --------------------------*/
335/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
Insertion Sort (section 2.7.2), as one sorts a hand of cards: is inserted in the sorted prefix ,...
The LIST of the book: a SEQUENCE [see ADT.h] realized as a bidirectional circular list of cells with ...
The checks of the contracts of the sorts [see Contract.h]: a copy of the input kept only when the aud...
a SEQUENCE realized as a bidirectional circular list with a sentinel, its cells from a pool if Pooled
Definition List.h:118
the storage on which the sorts of the book work in place, which read the items by their ranks only: a...
Definition Sorted.h:184
a range that the sorts take beside std::vector and std::span: random access, sized,...
Definition Sorted.h:153
void mergeSort(Store &&A, std::size_t cutoff=1, C less=C())
in increasing order, stable; the parts of at most items by insertionSort() (1 is the book)
void mergeSortBottomUp(Store &&A, C less=C())
the iterative Merge Sort: the runs of length are merged in pairs, for
long long countInversions(Store &&A, C less=C())
the number of inversions of , the pairs with , by Merge Sort, ; ends sorted
void mergeParts(Store &&A, std::vector< T > &B, std::size_t start, std::size_t mid, std::size_t end, C less=C(), long long *inv=nullptr)
and , sorted, merged through (the Merge() of the book); the inversions met are added to *inv,...
void mergeSortRec(Store &&A, std::vector< T > &B, std::size_t start, std::size_t end, std::size_t cutoff, C less=C(), long long *inv=nullptr)
the sort of (the MergeSort() of the book), insertionSort() on at most items; with also no merge of...
Definition ADT.h:96