ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
SequenceAlgorithms.h

Algorithms on a SEQUENCE, beyond the book, through its positions only, so that they work on a List and on an ArraySequence. More...

Files

file  ADS/sequence/SequenceAlgorithms.h
 Algorithms on a SEQUENCE [see ADT.h], beyond the book, through its positions only, so that they work on a List and on an ArraySequence:

Enumerations

enum class  ADS::Order {
  ADS::Order::EMPTY , ADS::Order::CONSTANT , ADS::Order::INCREASING , ADS::Order::DECREASING ,
  ADS::Order::NONDECREASING , ADS::Order::NONINCREASING , ADS::Order::NONE
}
 the order of a sequence, the most specific that holds More...

Functions

template<Sequence S>
long ADS::search (const S &s, const typename S::Item &v)
 the place of \( v \) counted from 0, or \( -( n + 1 ) \)
template<Sequence S>
long ADS::searchOrdered (const S &s, const typename S::Item &v)
 the place of \( v \) in the sorted \( s \), or \( -c \), \( c \) the comparisons made, -1 on the empty \( s \), so that a miss is always negative
template<Sequence S>
std::pair< typename S::Pos, bool > ADS::lowerBound (const S &s, const typename S::Item &v)
 the position of the first item not smaller than \( v \) in the sorted \( s \), or \( \mathit{pos}_n \), and whether it is \( v \): insert() of \( v \) there keeps \( s \) sorted
template<Sequence S>
S ADS::sublist (const S &s, typename S::Pos p, typename S::Pos q)
 the sequence of the items of \( s \) from the position \( p \) to the position \( q \), both included, empty if \( p \) is \( \mathit{pos}_n \)
template<Sequence S>
Order ADS::order (const S &s)
 the order of \( s \), the most specific that holds
template<Sequence S>
void ADS::bubbleSort (S &s)
 the bubble sort: at most \( n \) passes of swaps of adjacent items out of order, stopped at the first pass without swaps
template<Sequence S>
S ADS::mergeSorted (const S &a, const S &b)
 the merge of the sorted \( a \) and \( b \), the repeated items kept, those of \( a \) first among equals
template<Sequence S>
void ADS::naturalMergeSort (S &s, long *passes=nullptr)
 the natural merge sort; the number of passes goes to *passes, if given
template<Sequence S>
void ADS::removeDuplicates (S &s)
 keeps the first copy of each item, in \( O( n^2 ) \) comparisons

Detailed description

Algorithms on a SEQUENCE, beyond the book, through its positions only, so that they work on a List and on an ArraySequence.

The definitions, the invariants and the costs of the area are in Theory of sequences and lists.

Enumeration type documentation

◆ Order

enum class ADS::Order
strong

the order of a sequence, the most specific that holds

Enumerator
EMPTY 

no item

CONSTANT 

all the items equal

INCREASING 

each item larger than the one before

DECREASING 

each item smaller than the one before

NONDECREASING 

increasing, with some equal items

NONINCREASING 

decreasing, with some equal items

NONE 

neither increasing nor decreasing

Function documentation

◆ bubbleSort()

template<Sequence S>
void ADS::bubbleSort ( S & s)

the bubble sort: at most \( n \) passes of swaps of adjacent items out of order, stopped at the first pass without swaps

In pseudocode (section 12.2), by read() and write() only:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{bubbleSort}( s ) \cr 2 & \quad \mathit{swapped} \gets \textbf{true} \cr 3 & \quad \textbf{while}\ \mathit{swapped}\ \textbf{do}\quad \triangleright\ \text{a pass over } s \cr 4 & \qquad \mathit{swapped} \gets \textbf{false} \cr 5 & \qquad \textbf{foreach}\ \text{position } p \text{ with a successor } q = s.\mathrm{next}( p ) \text{, in order}\ \textbf{do} \cr 6 & \qquad\quad \textbf{if}\ s.\mathrm{read}( q ) < s.\mathrm{read}( p ) \ \textbf{then}\ \text{exchange the two items};\ \mathit{swapped} \gets \textbf{true} \end{array}} \]

Postcondition
\( s \) is sorted, with the same length (an audit)

◆ lowerBound()

template<Sequence S>
std::pair< typename S::Pos, bool > ADS::lowerBound ( const S & s,
const typename S::Item & v )

the position of the first item not smaller than \( v \) in the sorted \( s \), or \( \mathit{pos}_n \), and whether it is \( v \): insert() of \( v \) there keeps \( s \) sorted

In pseudocode (section 3.2.1):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{lowerBound}( s , v ) \cr 2 & \quad p \gets \text{the first position of } s \text{ that is finished or holds an item } \ge v \cr 3 & \quad \mathit{found} \gets \textbf{not}\ s.\mathrm{finished}( p ) \ \textbf{and}\ s.\mathrm{read}( p ) = v \cr 4 & \quad \textbf{return}\ ( p , \mathit{found} ) \end{array}} \]

Precondition
\( s \) is sorted (an audit)

◆ mergeSorted()

template<Sequence S>
S ADS::mergeSorted ( const S & a,
const S & b )

the merge of the sorted \( a \) and \( b \), the repeated items kept, those of \( a \) first among equals

In pseudocode (section 12.2), at most \( |a| + |b| - 1 \) comparisons:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{mergeSorted}( a , b ) \cr 2 & \quad c \gets \text{an empty sequence};\ ( p , q ) \gets ( a.\mathrm{head}() , b.\mathrm{head}() ) \cr 3 & \quad \textbf{while}\ \text{neither } p \text{ nor } q \text{ is finished}\ \textbf{do}\quad \triangleright \ \text{on ties, } a \text{ first} \cr 4 & \qquad \textbf{if}\ a.\mathrm{read}( p ) \le b.\mathrm{read}( q ) \ \textbf{then}\ \text{append } a.\mathrm{read}( p ) \text{ to } c; \ p \gets a.\mathrm{next}( p ) \cr 5 & \qquad \textbf{else}\ \text{append } b.\mathrm{read}( q ) \text{ to } c;\ q \gets b.\mathrm{next}( q ) \cr 6 & \quad \text{append to } c \text{ what is left of } a \text{, then what is left of } b \cr 7 & \quad \textbf{return}\ c \end{array}} \]

Precondition
\( a \) and \( b \) are sorted (an audit)
Postcondition
the result is sorted, of length \( |a| + |b| \) (an audit)

◆ naturalMergeSort()

template<Sequence S>
void ADS::naturalMergeSort ( S & s,
long * passes = nullptr )

the natural merge sort; the number of passes goes to *passes, if given

In pseudocode (section 12.2, by the runs of the sequence), each pass at least halving the runs:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{naturalMergeSort}( s ) \cr 2 & \quad \textbf{loop} \cr 3 & \qquad \text{deal the maximal nondecreasing runs of } s \text{ to } a \text{ and } b \text{ in turn} \cr 4 & \qquad r \gets \text{the number of runs};\ \textbf{if}\ r \le 1 \ \textbf{then return} \cr 5 & \qquad m \gets \text{an empty sequence} \cr 6 & \qquad \textbf{while}\ a \text{ or } b \text{ is not exhausted} \ \textbf{do} \cr 7 & \qquad\quad \text{append to } m \text{ the merge of the next run of } a \text{ and the next run of } b \cr 8 & \qquad s \gets m\quad \triangleright \ \text{about half the runs of before} \end{array}} \]

Postcondition
\( s \) is sorted, with the same length (an audit)

◆ order()

template<Sequence S>
Order ADS::order ( const S & s)

the order of \( s \), the most specific that holds

In pseudocode (section 3.2.1), one scan of the pairs of neighbors:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{order}( s ) \cr 2 & \quad \textbf{if}\ s.\mathrm{empty}()\ \textbf{then return} \ \text{EMPTY} \cr 3 & \quad \textbf{foreach}\ \text{two neighbors } x , y \text{ of } s \ \textbf{do}\ \text{note whether } x < y \text{, } y < x \text{ or } x = y \cr 4 & \quad \mathit{up} , \mathit{down} , \mathit{equal} \gets \text{whether each of the three was seen} \cr 5 & \quad \textbf{if}\ \mathit{up}\ \textbf{and}\ \mathit{down} \ \textbf{then return}\ \text{NONE} \cr 6 & \quad \textbf{if}\ \mathit{up}\ \textbf{then return}\ ( \text{NONDECREASING if } \mathit{equal} \text{, else INCREASING} ) \cr 7 & \quad \textbf{if}\ \mathit{down}\ \textbf{then return}\ ( \text{NONINCREASING if } \mathit{equal} \text{, else DECREASING} ) \cr 8 & \quad \textbf{return}\ \text{CONSTANT}\quad \triangleright \ \text{one item, or all equal} \end{array}} \]

◆ removeDuplicates()

template<Sequence S>
void ADS::removeDuplicates ( S & s)

keeps the first copy of each item, in \( O( n^2 ) \) comparisons

In pseudocode (section 3.2.1), each item compared with those after it:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{removeDuplicates}( s ) \cr 2 & \quad \textbf{foreach}\ \text{position } p \text{ of } s \text{, in order}\ \textbf{do}\quad \triangleright \ \text{the first copy stays} \cr 3 & \qquad \text{remove from } s \text{ every later position } q \text{ with } s.\mathrm{read}( q ) = s.\mathrm{read}( p ) \end{array}} \]

Postcondition
no item appears twice (an audit)

◆ search()

template<Sequence S>
long ADS::search ( const S & s,
const typename S::Item & v )

the place of \( v \) counted from 0, or \( -( n + 1 ) \)

In pseudocode (section 3.2.1, the scan of a sequence):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{search}( s , v ) \cr 2 & \quad p \gets s.\mathrm{head}();\ i \gets 0\quad \triangleright\ i \text{ the index of } p \cr 3 & \quad \textbf{while not}\ s.\mathrm{finished}( p )\ \textbf{and} \ s.\mathrm{read}( p ) \ne v\ \textbf{do}\ p \gets s.\mathrm{next}( p );\ i \gets i + 1 \cr 4 & \quad \textbf{if}\ s.\mathrm{finished}( p )\ \textbf{then return}\ -( i + 1 )\quad \triangleright\ i = n \cr 5 & \quad \textbf{return}\ i \end{array}} \]

◆ searchOrdered()

template<Sequence S>
long ADS::searchOrdered ( const S & s,
const typename S::Item & v )

the place of \( v \) in the sorted \( s \), or \( -c \), \( c \) the comparisons made, -1 on the empty \( s \), so that a miss is always negative

In pseudocode (section 3.2.1), the scan stopped at the first larger item:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{searchOrdered}( s , v ) \cr 2 & \quad p \gets s.\mathrm{head}();\ c \gets 0\quad \triangleright\ c \text{ the items read} \cr 3 & \quad \textbf{while not}\ s.\mathrm{finished}( p )\ \textbf{do} \cr 4 & \qquad x \gets s.\mathrm{read}( p );\ c \gets c + 1 \cr 5 & \qquad \textbf{if}\ v < x\ \textbf{then return}\ -c\quad \triangleright\ v \text{ would go before } x \cr 6 & \qquad \textbf{if}\ v = x\ \textbf{then return}\ c - 1 \cr 7 & \qquad p \gets s.\mathrm{next}( p ) \cr 8 & \quad \textbf{return}\ -\max( c , 1 ) \end{array}} \]

Precondition
\( s \) is sorted (an audit)

◆ sublist()

template<Sequence S>
S ADS::sublist ( const S & s,
typename S::Pos p,
typename S::Pos q )

the sequence of the items of \( s \) from the position \( p \) to the position \( q \), both included, empty if \( p \) is \( \mathit{pos}_n \)

In pseudocode (section 3.2.1), the items appended to \( r \) at its end, whose position is kept:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{sublist}( s , p , q ) \cr 2 & \quad r \gets \text{an empty sequence} \cr 3 & \quad \textbf{while not}\ s.\mathrm{finished}( p )\ \textbf{do}\quad \triangleright\ \text{from } p \text{ to } q \text{ included} \cr 4 & \qquad \text{append } s.\mathrm{read}( p ) \text{ to } r; \ \textbf{if}\ p = q\ \textbf{then}\ \textbf{break} \cr 5 & \qquad p \gets s.\mathrm{next}( p ) \cr 6 & \quad \textbf{return}\ r \end{array}} \]

Precondition
\( q \) is \( p \) or follows it, and holds an item unless \( p \) is \( \mathit{pos}_n \)