alglib::sort Namespace Reference

Functions

template<typename ForwardIter , typename BinaryPred >
void selection_sort (ForwardIter first, ForwardIter last, const BinaryPred &LT)
 
template<typename ForwardIter >
void selection_sort (ForwardIter first, ForwardIter last)
 
template<typename RandomAccessIter , typename BinaryPred >
RandomAccessIter randomized_partition (RandomAccessIter first, RandomAccessIter last, const BinaryPred &LT)
 
template<typename RandomAccessIter , typename BinaryPred >
void quick_sort (RandomAccessIter first, RandomAccessIter last, const BinaryPred &LT)
 
template<typename RandomAccessIter >
void quick_sort (RandomAccessIter first, RandomAccessIter last)
 
template<typename RandomAccessIter , typename BinaryPred >
void merge_sort (RandomAccessIter first, RandomAccessIter last, const BinaryPred &LT)
 
template<typename RandomAccessIter >
void merge_sort (RandomAccessIter first, RandomAccessIter last)
 
template<typename RandomAccessIter , typename BinaryPred >
void heap_sort (RandomAccessIter first, RandomAccessIter last, const BinaryPred &LT)
 
template<typename RandomAccessIter >
void heap_sort (RandomAccessIter first, RandomAccessIter last)
 

Function Documentation

template<typename RandomAccessIter , typename BinaryPred >
void alglib::sort::heap_sort ( RandomAccessIter  first,
RandomAccessIter  last,
const BinaryPred &  LT 
)
147  {
148  typedef
149  typename std::iterator_traits<RandomAccessIter>::value_type val_type;
150 
152 
153  for (RandomAccessIter it = first; it != last; it++)
154  H.insert(*it);
155 
156  for (RandomAccessIter it = first; it != last; it++) {
157  *it = H.get_min();
158  H.delete_min();
159  }
160 }
void delete_min()
Definition: binary_heap.h:224
void insert(const elt_t &elt)
Definition: binary_heap.h:218
Definition: binary_heap.h:213
const elt_t & get_min() const
Definition: binary_heap.h:221
template<typename RandomAccessIter >
void alglib::sort::heap_sort ( RandomAccessIter  first,
RandomAccessIter  last 
)
164  {
165  heap_sort(first, last, std::less<typename RandomAccessIter::value_type>());
166 }
void heap_sort(RandomAccessIter first, RandomAccessIter last)
Definition: sort.h:164
template<typename RandomAccessIter , typename BinaryPred >
void alglib::sort::merge_sort ( RandomAccessIter  first,
RandomAccessIter  last,
const BinaryPred &  LT 
)
125  {
126  int sz = std::distance(first, last);
127  if (sz > 1) {
128  std::vector<typename RandomAccessIter::value_type>
129  subarr1(first, first + sz / 2);
130  std::vector<typename RandomAccessIter::value_type>
131  subarr2(first + sz / 2, last);
132  merge_sort(subarr1.begin(), subarr1.end(), LT);
133  merge_sort(subarr2.begin(), subarr2.end(), LT);
134  std::merge(subarr1.begin(), subarr1.end(),
135  subarr2.begin(), subarr2.end(), first);
136  }
137 }
void merge_sort(RandomAccessIter first, RandomAccessIter last)
Definition: sort.h:140
template<typename RandomAccessIter >
void alglib::sort::merge_sort ( RandomAccessIter  first,
RandomAccessIter  last 
)
140  {
141  merge_sort(first, last, std::less<typename RandomAccessIter::value_type>());
142 }
void merge_sort(RandomAccessIter first, RandomAccessIter last)
Definition: sort.h:140
template<typename RandomAccessIter , typename BinaryPred >
void alglib::sort::quick_sort ( RandomAccessIter  first,
RandomAccessIter  last,
const BinaryPred &  LT 
)
108  {
109  if (first < last) {
110  RandomAccessIter pivot = randomized_partition(first, last, LT);
111  quick_sort(first, pivot, LT);
112  quick_sort(pivot + 1, last, LT);
113  }
114 }
RandomAccessIter randomized_partition(RandomAccessIter first, RandomAccessIter last, const BinaryPred &LT)
Definition: sort.h:82
void quick_sort(RandomAccessIter first, RandomAccessIter last)
Definition: sort.h:118
template<typename RandomAccessIter >
void alglib::sort::quick_sort ( RandomAccessIter  first,
RandomAccessIter  last 
)
118  {
119  quick_sort(first, last, std::less<typename RandomAccessIter::value_type>());
120 }
void quick_sort(RandomAccessIter first, RandomAccessIter last)
Definition: sort.h:118
template<typename RandomAccessIter , typename BinaryPred >
RandomAccessIter alglib::sort::randomized_partition ( RandomAccessIter  first,
RandomAccessIter  last,
const BinaryPred &  LT 
)
84  {
85  /* The function selects a random element in [first, last) as pivot and
86  * partitions the interval around it.
87  * The part
88  */
89  int sz = std::distance(first, last);
90 
91  RandomAccessIter it = first + rand() % sz; // O(n) cost
92  std::swap(*it, *(last - 1));
93  auto pivot = last - 1;
94 
95  RandomAccessIter sep = first; // sep heads the larger-elements partition
96  for (auto it = first; it != last; it++) {
97  if (*it < *pivot) {
98  std::swap(*it, *sep);
99  sep++;
100  }
101  }
102  std::swap(*pivot, *sep);
103  return sep;
104 }
template<typename ForwardIter , typename BinaryPred >
void alglib::sort::selection_sort ( ForwardIter  first,
ForwardIter  last,
const BinaryPred &  LT 
)
42  {
43  for (auto it = first; it != last; it++)
44  std::swap(*it, *std::min_element(it, last, LT));
45 
46  /* Analysis
47  * Proof of Correctness:
48  * We prove it using the following invariant:
49  * Prior to an iteration of the loop, A[first..it-1] contains
50  * the smallest (it-first) elements of A in a non-decreasing order.
51  * Initialization: Initially, it = first, so A[first..first-1] contains
52  * no element. Hence it is trivially sorted.
53  * Maintenance: A[first..it-1] is sorted prior to an iteration, and in
54  * the loop A[it] is set to a minimum element in A[it..last].
55  * A[it] >= E, E ϵ A[first..it-1] because prior to the iteration,
56  * A[it..last] did not contain any element greater than E,
57  * E ϵ A[first..it-1]. Hence, A[first..it] contains its elements in a
58  * non-decreasing order. Further, the range also contains the smallest
59  * (it-first+1) elemnts of A since one more is added in the loop.
60  * Termination: At the end, we have, A[first..last] contains all the
61  * the elemets in a non-decreasing order. This proves the claim.
62  *
63  * Stable: Yes (Depends on the implementation of min_element. If it
64  * returns the first minimum element, then it is stable.)
65  * In-place: Yes
66  * Time-Complexity Gurantee: O(n^2).
67  * The loop runs n times and each of the iteration invoke min_element,
68  * which takes O(i) time where i goes from 1 to n.
69  * Mutual-Exclusion: required.
70  */
71 }
template<typename ForwardIter >
void alglib::sort::selection_sort ( ForwardIter  first,
ForwardIter  last 
)
inline
75  {
76  selection_sort(first, last, std::less<typename ForwardIter::value_type>());
77 }
void selection_sort(ForwardIter first, ForwardIter last)
Definition: sort.h:75