#include<iostream>
#include<windows.h>
using namespace std;
#define M 100000
#define K 10
void AdjustDown(int parent, int arr[])
{
int child = parent * 2 + 1;
while (child<K)
{
if (child+1<K&&arr[child]>arr[child + 1] )
{
child += 1;
}
if (arr[parent]>arr[child])
{
swap(arr[parent], arr[child]);
parent = child;
child = parent * 2 + 1;
}
else
break;
}
}
void GetKMaxNum(int array[], int top[])
{
for (int idx = 0; idx < K; idx++)
{
array[idx] = top[idx];
}
for (int idx = (K - 2) >> 1; idx>=0; --idx)
{
AdjustDown(idx, top);
}
for (int idx = K ; idx <M; ++idx)
{
if (top[0] < array[idx])
{
top[0] = array[idx];
AdjustDown(0, top);
}
}
}
void Print(int top[])
{
int count = 0;
for (int i = 0; i < K; i++)
{
cout << top[i] << " ";
count++;
if (count % 5 == 0)
{
cout << endl;
}
}
cout << endl;
}
void Test()
{
int array[M] = { 0 };
int top[K] = { 0 };
for (int i = 0; i < M; i++)
{
array[i] = i;
}
array[10000] = 9999999;
array[M-100] = 199999;
array[M-5] = 22222222;
GetKMaxNum(array, top);
Print(top);
}
int main()
{
Test();
system("pause");
return 0;
}