3 #ifndef CH_TOOLS__HEAPSORT_HXX 4 #define CH_TOOLS__HEAPSORT_HXX 48 template<
class I,
class V>
void heapify(
int i,
int n, I& ind,
const V& val,
bool non_decreasing=
true);
56 template<
class I,
class V>
void build_heap(
int n, I& ind,
const V& val,
bool non_decreasing=
true);
65 template<
class I,
class V>
void heapsort(
int n, I& ind,
const V& val,
bool non_decreasing=
true);
74 template<
class I,
class V>
void heapify(
int i,
int n, I* ind,
const V* val,
bool non_decreasing=
true);
82 template<
class I,
class V>
void build_heap(
int n, I* ind,
const V* val,
bool non_decreasing=
true);
90 template<
class I,
class V>
void heapsort(
int n, I* ind,
const V* val,
bool non_decreasing=
true);
100 template<
class I,
class V>
inline void heapify(
int i,
int n,I& ind,
const V& val,
bool non_decreasing)
108 if (val[ind[k]]<val[ind[2*k+1]])
109 swap(ind[k],ind[2*k+1]);
112 if ((val[ind[k]]<val[ind[2*k+1]])||
113 (val[ind[k]]<val[ind[2*k+2]])){
114 if (val[ind[2*k+1]]>val[ind[2*k+2]]) j=2*k+1;
126 if (val[ind[k]]>val[ind[2*k+1]])
127 swap(ind[k],ind[2*k+1]);
130 if ((val[ind[k]]>val[ind[2*k+1]])||
131 (val[ind[k]]>val[ind[2*k+2]])){
132 if (val[ind[2*k+1]]<val[ind[2*k+2]]) j=2*k+1;
142 template<
class I,
class V>
inline void build_heap(
int n,I& ind,
const V& val,
bool non_decreasing)
144 for(
int i=n/2;--i>=0;)
heapify(i,n,ind,val,non_decreasing);
147 template<
class I,
class V>
inline void heapsort(
int n,I& ind,
const V& val,
bool non_decreasing)
158 heapify(0,i,ind,val,non_decreasing);
162 template<
class I,
class V>
inline void heapify(
int i,
int n,I* ind,
const V* val,
bool non_decreasing)
171 if (valindi<val[ind[j]]){
207 if (valindi>val[ind[j]]){
240 template<
class I,
class V>
inline void build_heap(
int n,I* ind,
const V* val,
bool non_decreasing)
242 for(
int i=n/2;--i>=0;)
heapify(i,n,ind,val,non_decreasing);
245 template<
class I,
class V>
inline void heapsort(
int n,I* ind,
const V* val,
bool non_decreasing)
256 heapify(0,i,ind,val,non_decreasing);
void build_heap(int n, I &ind, const V &val, bool non_decreasing=true)
Build a heap out of ind[0] to ind[n-1], so that val[ind[j]]>=max(val[ind[2*j+1]],val[ind[2*j+2]] for ...
Definition: heapsort.hxx:142
void swap(double &a, double &b)
swaps two double variables
Definition: mymath.hxx:79
void heapsort(int n, I &ind, const V &val, bool non_decreasing=true)
Sort ind[0]...ind[n-1] so that val[ind[i]]<=val[ind[j]] for 0<=i<j<n.
Definition: heapsort.hxx:147
Header defining simple functions like max, min, abs.
Matrix Classes and Linear Algebra. See Matrix Classes (namespace CH_Matrix_Classes) for a quick intro...
Definition: PSCOracle.hxx:20
void heapify(int i, int n, I &ind, const V &val, bool non_decreasing=true)
Heapify element i (in 0,...,n-1) in ind, so that afterwards val[ind[j]]>=max(val[ind[2*j+1]],val[ind[2*j+2]] for the subtree with root i.
Definition: heapsort.hxx:100