huc::sort Namespace Reference

Classes

class  MergeInPlace
 
class  MergeWithBuffer
 

Functions

template<typename IT , typename Compare = std::less<typename std::iterator_traits<IT>::value_type>>
void Bubble (const IT &begin, const IT &end)
 
template<typename IT , typename Compare = std::less<typename std::iterator_traits<IT>::value_type>>
void Cocktail (const IT &begin, const IT &end)
 
template<typename IT , typename Compare = std::less<typename std::iterator_traits<IT>::value_type>>
void Comb (const IT &begin, const IT &end)
 
template<typename IT , typename Aggregator = MergeWithBuffer<IT>>
void MergeSort (const IT &begin, const IT &end)
 
template<typename IT , typename Compare = std::less_equal<typename std::iterator_traits<IT>::value_type>>
IT Partition (const IT &begin, const IT &pivot, const IT &end)
 
template<typename IT , typename Compare = std::less_equal<typename std::iterator_traits<IT>::value_type>>
void QuickSort (const IT &begin, const IT &end)
 
template<typename IT >
void RaddixSort (const IT &begin, const IT &end, unsigned int base=10)
 

Function Documentation

template<typename IT , typename Compare = std::less<typename std::iterator_traits<IT>::value_type>>
void huc::sort::Bubble ( const IT &  begin,
const IT &  end 
)

Bubble Sort - Proceed an in-place sort on the elements.

Template Parameters
ITtype using to go through the collection.
Comparefunctor type (std::less_equal in order, std::greater_equal for inverse order).
Parameters
begin,enditerators to the initial and final positions of the sequence to be sorted. The range used is [first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.
Returns
void.

Definition at line 41 of file bubble.hxx.

42  {
43  const auto distance = static_cast<const int>(std::distance(begin, end));
44  if (distance < 2)
45  return;
46 
47  int endIdx = -1;
48  bool hasSwapped;
49  // for each element - bubble it up until the end.
50  for (auto it = begin; it < end - 1; ++it, --endIdx)
51  {
52  hasSwapped = false;
53  for (auto curIt = begin; curIt < end + endIdx; ++curIt)
54  if (Compare()(*(curIt + 1), *curIt))
55  {
56  std::swap(*(curIt + 1), *curIt);
57  hasSwapped = true;
58  }
59 
60  if (!hasSwapped)
61  break;
62  }
63  }
template<typename IT , typename Compare = std::less<typename std::iterator_traits<IT>::value_type>>
void huc::sort::Cocktail ( const IT &  begin,
const IT &  end 
)

Cocktail Sort - Proceed an in-place sort on the elements.

Template Parameters
ITtype using to go through the collection.
Comparefunctor type (std::less_equal in order, std::greater_equal for inverse order).
Parameters
begin,enditerators to the initial and final positions of the sequence to be sorted. The range used is [first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.
Returns
void.

Definition at line 41 of file cocktail.hxx.

42  {
43  const auto distance = static_cast<const int>(std::distance(begin, end));
44  if (distance < 2)
45  return;
46 
47  int beginIdx = 0;
48  int endIdx = distance - 1;
49  bool hasSwapped = true;
50  while (hasSwapped && beginIdx < distance - 1)
51  {
52  hasSwapped = false;
53  // for each element from beginning - bubble it up until the end.
54  for (auto it = begin + beginIdx; it < begin + endIdx; ++it)
55  if (Compare()(*(it + 1), *it))
56  {
57  std::swap(*it, *(it + 1));
58  hasSwapped = true;
59  }
60  --endIdx;
61 
62  if (!hasSwapped)
63  break;
64 
65  // for each element from the end- bubble it down until the beginning.
66  for (auto it = begin + endIdx - 1; it >= begin + beginIdx; --it)
67  if (Compare()(*(it + 1), *it))
68  {
69  std::swap(*it, *(it + 1));
70  hasSwapped = true;
71  }
72 
73  ++beginIdx;
74  }
75  }
template<typename IT , typename Compare = std::less<typename std::iterator_traits<IT>::value_type>>
void huc::sort::Comb ( const IT &  begin,
const IT &  end 
)

Comb Sort - Proceed an in-place sort on the elements.

Template Parameters
ITtype using to go through the collection.
Comparefunctor type (std::less_equal in order, std::greater_equal for inverse order).
Parameters
begin,enditerators to the initial and final positions of the sequence to be sorted. The range used is [first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.
Returns
void.

Definition at line 41 of file comb.hxx.

42  {
43  const auto distance = static_cast<const int>(std::distance(begin, end));
44  if (distance < 2)
45  return;
46 
47  auto gap = distance;
48  double shrink = 1.3;
49  bool hasSwapped = true;
50  while (hasSwapped)
51  {
52  hasSwapped = false;
53 
54  gap /= shrink;
55  if (gap > 1)
56  hasSwapped = true;
57  else
58  gap = 1;
59 
60  for (auto it = begin; it + gap < end; ++it)
61  if (Compare()(*(it + gap), *it))
62  {
63  std::swap(*it, *(it + gap));
64  hasSwapped = true;
65  }
66  }
67  }
template<typename IT , typename Aggregator = MergeWithBuffer<IT>>
void huc::sort::MergeSort ( const IT &  begin,
const IT &  end 
)

MergeSort - Proceed sort on the elements whether using an in-place strategy or using a buffer one.

Template Parameters
ITtype using to go through the collection.
Aggregatorfunctor type used to aggregate two sorted sequences.
Parameters
begin,enditerators to the initial and final positions of the sequence to be sorted. The range used is [first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.
Returns
void.

Definition at line 141 of file merge.hxx.

142  {
143  const auto ksize = static_cast<const int>(std::distance(begin, end));
144  if (ksize < 2)
145  return;
146 
147  auto pivot = begin + ksize / 2;
148 
149  // Recursively break the vector into two pieces
150  MergeSort<IT, Aggregator>(begin, pivot);
151  MergeSort<IT, Aggregator>(pivot, end);
152 
153  // Merge the two pieces
154  Aggregator()(begin, pivot, end);
155  }
template<typename IT , typename Compare = std::less_equal<typename std::iterator_traits<IT>::value_type>>
IT huc::sort::Partition ( const IT &  begin,
const IT &  pivot,
const IT &  end 
)

Partition-Exchange - Proceed an in-place patitionning on the elements.

Template Parameters
ITtype using to go through the collection.
Comparefunctor type (std::less_equal for smaller elements in left partition, std::greater_equal for greater elements in left partition).
Parameters
begin,endconst iterators to the initial and final positions of the sequence to be pivoted. The range used is [first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.
pivotiterator on which the partition is delimited between begin and end.
Returns
new pivot iterator.

Definition at line 43 of file partition.hxx.

44  {
45  if (std::distance(begin, end) < 2 || pivot == end)
46  return pivot;
47 
48  auto pivotValue = *pivot; // Keep the pivot value;
49  std::swap(*pivot, *(end - 1)); // Put the pivot at the end for convenience
50  auto store = begin; // Put the store pointer at the beginning
51 
52  // Swap each smaller before the pivot item
53  for (auto it = begin; it != end - 1; ++it)
54  {
55  if (Compare()(*it, pivotValue))
56  {
57  std::swap(*store, *it);
58  ++store;
59  }
60  }
61 
62  // Replace the pivot at its good position
63  std::swap(*(end - 1), *store);
64 
65  return store;
66  }
template<typename IT , typename Compare = std::less_equal<typename std::iterator_traits<IT>::value_type>>
void huc::sort::QuickSort ( const IT &  begin,
const IT &  end 
)

Quick Sort - Proceed an in-place sort on the elements.

Template Parameters
ITtype using to go through the collection.
Comparefunctor type (std::less_equal in order, std::greater_equal for inverse order).
Parameters
begin,enditerators to the initial and final positions of the sequence to be sorted. The range used is [first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.
Returns
void.

Definition at line 40 of file quick.hxx.

41  {
42  const auto distance = static_cast<const int>(std::distance(begin, end));
43  if (distance < 2)
44  return;
45 
46  auto pivot = begin + (rand() % distance); // Pick Random Pivot € [begin, end]
47  auto newPivot = Partition<IT, Compare>(begin, pivot, end); // Proceed partition
48 
49  QuickSort<IT, Compare>(begin, newPivot); // Recurse on first partition
50  QuickSort<IT, Compare>(newPivot + 1, end); // Recurse on second partition
51  }
template<typename IT >
void huc::sort::RaddixSort ( const IT &  begin,
const IT &  end,
unsigned int  base = 10 
)

LSD Raddix Sort - Non-comparative integer sorting algorithm Proceed a raddix-sort on the elements contained in [begin, end[

Warning
Works properly only with integral type of non-negative values.
Template Parameters
ITtype using to go through the collection.
Parameters
begin,enditerators to the initial and final positions of the sequence to be sorted. The range used is [first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.
basebase in which the numbers are represented.
Returns
void.

Definition at line 45 of file raddix.hxx.

46  {
47  if (std::distance(begin, end) < 2)
48  return;
49 
50  // Create a bucket for each possible value of a digit
51  std::vector<std::queue<typename std::iterator_traits<IT>::value_type>> buckets(base);
52 
53  // For all possible digit
54  for (size_t powBase = 1;
55  powBase < std::numeric_limits<typename std::iterator_traits<IT>::value_type>::max();
56  powBase *= base)
57  {
58  // Push each number into the bucket of its digit value
59  for (auto it = begin; it != end; ++it)
60  buckets[static_cast<int>(*it / powBase) % base].push(*it);
61 
62  // Dequeu back all the values
63  auto itSrc = begin;
64  for (auto it = buckets.begin(); it != buckets.end(); ++it)
65  while (!it->empty())
66  {
67  *(itSrc++) = it->front();
68  it->pop();
69  }
70  }
71  }