代码拉取完成,页面将自动刷新
// heap6.hxx
#ifndef _BLOGS_HEAP6
#define _BLOGS_HEAP6
#include <type_traits>
#include "array.hxx"
#include "comparable.hxx"
#include "std_out.hxx"
namespace blogs
{
template
<
int _d
>
class Heap6
{
private:
template
<
class _T,
class = typename std::enable_if<std::is_base_of<Comparable<_T>, _T>::value>::type
>
static
void
__sink__(Array<_T> & a, int k, int n)
{
while (_d*k-(_d-2) <= n) {
int j = _d*k-(_d-2);
for (int i = j+1; i <= n && i <= _d*k+1; ++i)
if (__less__(a[j-1], a[i-1])) j = i;
if (!__less__(a[k-1], a[j-1])) break;
__exch__(a, k, j);
k = j;
}
}
template
<
class _T,
class = typename std::enable_if<std::is_base_of<Comparable<_T>, _T>::value>::type
>
static
bool
__less__(_T const& v, _T const& w)
{
return v.compare_to(w) < 0;
}
template
<
class _T,
class = typename std::enable_if<std::is_base_of<Comparable<_T>, _T>::value>::type
>
static
void
__exch__(Array<_T> & a, int i, int j)
{
_T t = a[i-1];
a[i-1] = a[j-1];
a[j-1] = t;
}
public:
template
<
class _T,
class = typename std::enable_if<std::is_base_of<Comparable<_T>, _T>::value>::type
>
static
void
sort(Array<_T> & a)
{
int N = a.size();
for (int k = (N+_d-2)/_d; k >= 1; --k)
__sink__(a, k, N);
while (N > 1) {
__exch__(a, 1, N);
--N;
__sink__(a, 1, N);
}
}
template
<
class _T,
class = typename std::enable_if<std::is_base_of<Comparable<_T>, _T>::value>::type
>
static
bool
is_sorted(Array<_T> const& a)
{
int N = a.size();
for (int i = 1; i < N; ++i)
if (__less__(a[i], a[i-1])) return false;
return true;
}
};
} // end of namespace blogs
#endif
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。