#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
const int N = 10000;
const int K = 10;
void AdjustDown(int* a)
{
int father = 0;
int child = (a[father * 2 + 1] < a[father * 2 + 2] ? (father * 2 + 1) : (father * 2 + 2));
while (a[father] > a[child] && child < K)
{
swap(a[father], a[child]);
father = child;
if (father * 2 + 1 == K - 1)
child = father * 2 + 1;
if(father*2+1 < K-1)
child = (a[father * 2 + 1] < a[father * 2 + 2] ? (father * 2 + 1) : (father * 2 + 2));
}
}
int main()
{
int a[N] = { 0 };
for (int i = 0; i < N; ++i)
{
a[i] = rand()%100;
}
a[54] = 111;
a[542] = 122;
a[2] = 3124;
a[393] = 999;
a[333] = 888;
a[1001] = 789;
a[3214] = 1131;
a[3245] = 1115;
a[3924] = 911;
a[112] = 112;
make_heap(a, a+K,greater<int>());
cout << "make_heap:" << endl;
for (int i = 0; i < K; ++i)
{
cout << a[i] << " ";
}
cout << endl;
for (int i = K; i < N; ++i)
{
if (a[i] >= a[0])
{
a[0] = a[i];
AdjustDown(&a[0]);
}
}
cout << "make_heap:" << endl;
for (int i = 0; i < K; ++i)
{
cout << a[i] << " ";
}
cout << endl;
system("pause");
return 0;
}

void HeadSort(int* a, size_t n)
{
for (int i = n; i > 0; i--)
{
make_heap(a, a+i);
swap(a[0], a[i - 1]);
}
}
int main()
{
int a[10] = { 0 };
for (int i = 0; i < 10; ++i)
{
a[i] = 10 - i;
}
HeadSort(a, 10);
for (int i = 0; i < 10; ++i)
{
cout << a[i] << " ";
}
cout << endl;
system("pause");
return 0;
}
