|
ADS
Algorithms and Data Structures
|
Searches in sorted vectors beyond the place of a given item. More...
Files | |
| file | ADS/search/SortedSearches.h |
| Searches in sorted vectors beyond the place of a given item [see BinarySearch.h]: the minimum of a rotated vector, the item closest to a value, two items with a given sum, and the item of a given rank in the union of two sorted vectors, whose middle ranks are the medians. | |
Functions | |
| template<class T, class C = std::less<>> | |
| std::size_t | ADS::rotatedMinimum (const std::vector< T > &A, C less=C()) |
| the place of the minimum of \( A \), a strictly increasing vector rotated right by that number of places; \( O( \log n ) \) | |
| template<class T> requires ( std::is_arithmetic_v< T > && ! std::is_same_v< T , bool > ) | |
| std::size_t | ADS::closestValue (const std::vector< T > &A, const T &x) |
| the first place of an item of the sorted \( A \) at the least distance from \( x \), the smaller item on ties; \( O( \log n ) \) | |
| template<class T, class S> | |
| std::optional< std::pair< std::size_t, std::size_t > > | ADS::pairWithSum (const std::vector< T > &A, const S &x) |
| two places \( i < j \) of the sorted \( A \) with \( A[ i ] + A[ j ] = x \), the sum computed in the type S, or nothing; \( O( n ) \) | |
| template<class T, class C = std::less<>> | |
| T | ADS::kthOfTwoSorted (const std::vector< T > &X, const std::vector< T > &Y, std::size_t k, C less=C()) |
| the item of rank \( k \), from 0, of the sorted \( X \) and \( Y \) together; \( O( \log \min( m , n ) ) \) | |
Searches in sorted vectors beyond the place of a given item.
The definitions, the invariants and the costs of the area are in Theory of searching.
| std::size_t ADS::closestValue | ( | const std::vector< T > & | A, |
| const T & | x ) |
the first place of an item of the sorted \( A \) at the least distance from \( x \), the smaller item on ties; \( O( \log n ) \)
In pseudocode (Esercizi Divide-et-impera of the book), the distances \( | a - b | \) computed with no overflow:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{closestValue}( A , x ) \cr 2 & \quad r \gets \mathrm{lowerBound}( A , x )\quad \triangleright\ A[ r ] \text{ is the first } \ge x \cr 3 & \quad \textbf{if}\ r = n\ \textbf{then return}\ \mathrm{lowerBound}( A , A[ n - 1 ] ) \cr 4 & \quad \textbf{if}\ r = 0\ \textbf{or}\ A[ r ] - x < x - A[ r - 1 ] \ \textbf{then return}\ r \cr 5 & \quad \textbf{return}\ \mathrm{lowerBound}( A , A[ r - 1 ] )\quad \triangleright\ \text{the first copy} \end{array}} \]
| T ADS::kthOfTwoSorted | ( | const std::vector< T > & | X, |
| const std::vector< T > & | Y, | ||
| std::size_t | k, | ||
| C | less = C() ) |
the item of rank \( k \), from 0, of the sorted \( X \) and \( Y \) together; \( O( \log \min( m , n ) ) \)
In pseudocode (Frederickson and Johnson, beyond the book), \( S \) the shorter of the two vectors and \( L \) the other one, of \( m \le n \) items, and \( i \) the items of \( S \) among the \( k + 1 \) smallest:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{kthOfTwoSorted}( X , Y , k ) \cr 2 & \quad \mathit{lo} \gets \max\{ 0 , k + 1 - n \};\ \mathit{hi} \gets \min\{ k + 1 , m \} \cr 3 & \quad \textbf{while}\ \mathit{lo} < \mathit{hi}\ \textbf{do} \cr 4 & \qquad i \gets \mathit{lo} + \lfloor ( \mathit{hi} - \mathit{lo} ) / 2 \rfloor;\ j \gets k + 1 - i \cr 5 & \qquad \textbf{if}\ S[ i ] < L[ j - 1 ]\ \textbf{then}\ \mathit{lo} \gets i + 1\ \textbf{else}\ \mathit{hi} \gets i \cr 6 & \quad i \gets \mathit{lo};\ j \gets k + 1 - i \cr 7 & \quad \textbf{return}\ \text{the larger of } S[ i - 1 ] \text{ and } L[ j - 1 ] \text{, those that exist} \end{array}} \]
| std::optional< std::pair< std::size_t, std::size_t > > ADS::pairWithSum | ( | const std::vector< T > & | A, |
| const S & | x ) |
two places \( i < j \) of the sorted \( A \) with \( A[ i ] + A[ j ] = x \), the sum computed in the type S, or nothing; \( O( n ) \)
In pseudocode (Esercizi vari of the book, on one vector instead of two), no pair out of \( A[ i .. j ] \) having the sum \( x \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{pairWithSum}( A , x ) \cr 2 & \quad ( i , j ) \gets ( 0 , n - 1 )\quad \triangleright\ \text{no pair out of } [ i , j ] \text{ sums to } x \cr 3 & \quad \textbf{while}\ i < j\ \textbf{do} \cr 4 & \qquad s \gets A[ i ] + A[ j ] \cr 5 & \qquad \textbf{if}\ s < x\ \textbf{then}\ i \gets i + 1\ \textbf{else if}\ s > x\ \textbf{then}\ j \gets j - 1\ \textbf{else return}\ ( i , j ) \cr 6 & \quad \textbf{return nil} \end{array}} \]
| std::size_t ADS::rotatedMinimum | ( | const std::vector< T > & | A, |
| C | less = C() ) |
the place of the minimum of \( A \), a strictly increasing vector rotated right by that number of places; \( O( \log n ) \)
In pseudocode (Esercizi Divide-et-impera of the book), the minimum in \( A[ \mathit{lo} .. \mathit{hi} ] \) at all times:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{rotatedMinimum}( A ) \cr 2 & \quad ( \mathit{lo} , \mathit{hi} ) \gets ( 0 , n - 1 )\quad \triangleright\ \text{the minimum is in } A[ \mathit{lo} .. \mathit{hi} ] \cr 3 & \quad \textbf{while}\ \mathit{lo} < \mathit{hi}\ \textbf{do} \cr 4 & \qquad m \gets \mathit{lo} + \lfloor ( \mathit{hi} - \mathit{lo} ) / 2 \rfloor \cr 5 & \qquad \textbf{if}\ A[ m ] \le A[ \mathit{hi} ]\ \textbf{then} \ \mathit{hi} \gets m\ \textbf{else}\ \mathit{lo} \gets m + 1 \cr 6 & \quad \textbf{return}\ \mathit{lo} \end{array}} \]