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

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: More...

#include "ADS/ADT.h"
#include "ADS/Contract.h"
#include "ADS/sort/Sorted.h"
#include <cstddef>
#include <utility>
#include "ADS/sequence/impl/SequenceAlgorithms.tpp"

Go to the source code of this file.

Namespaces

namespace  ADS

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 [see ADT.h], beyond the book, through its positions only, so that they work on a List and on an ArraySequence:

  • search(), the place of an item counted from 0, or \( -( n + 1 ) \) after \( n \) comparisons, and searchOrdered() on a sorted sequence, which stops at the first larger item and gives \( -c \), \( c \) the comparisons made, when it misses;
  • lowerBound(), the position of the first item not smaller than \( v \) in a sorted sequence, i.e., where \( v \) goes to keep it sorted, and whether \( v \) is there, so that a search and an insertion share the same scan;
  • sublist(), the items from a position to another one, the empty sequence being a sublist of every sequence;
  • order(), which tells whether a sequence is empty, constant, strictly or weakly increasing or decreasing, or none of them;
  • bubbleSort(), \( O( n^2 ) \) swaps of adjacent items by read() and write();
  • naturalMergeSort(), the natural merge sort: the maximal sorted runs are distributed alternately to two sequences and merged in pairs back, until a single run is left, in \( O( n \log r ) \) time for \( r \) runs and \( O( n ) \) on a sorted sequence;
  • mergeSorted(), the merge of two sorted sequences that keeps the repeated items, and removeDuplicates(), which keeps the first copy of each item in \( O( n^2 ) \).

The contracts [see Contract.h]: searchOrdered() and mergeSorted() need sorted sequences, and the sorts, the merge and removeDuplicates() ensure a sorted result, or one without repetitions, of the right length; all of them cost \( O( n ) \) or more, and are audits, whose comparisons a comparator that counts its calls can skip [see checkingContract()]. The algorithms are defined in SequenceAlgorithms.tpp.

See also
A. Montresor, Algoritmi e Strutture Dati, 2025, sections 3.2.1 and 12.2, https://algoritmicamente.it/materiali/libro/capitoli/cap12.pdf
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa