just hold on！

###### 堆的简单应用

#include<iostream>
#include<algorithm>

using namespace std;
//1.在N个数据当中找出最大的K个数

const int N = 10000;
const int K = 100;

void AdjustDown1(int a[], int size, int parent)  //建一个小堆
{
int child = parent * 2 + 1;

while (child < K)
{
if ((child + 1 < K) && (a[child + 1] < a[child]))
{
child++;
}

if (a[child] < a[parent])
{
swap(a[child], a[parent]);
parent = child;
child = parent * 2 + 1;
}
else
{
break;
}
}

}

void GetTopK(int a[],int TopK[])
{
assert(K < N);
int i = 0;
int j = 0;
int m = 0;
int n = 0;

for (i = 0; i < K; i++)
{
TopK[i] = a[i];     //取出a中的前k个数字放到topk[]里面
}

//建堆

for (j = (K - 2) / 2; j >0; --j)
{
}

for (int m = 0; m < N; ++m)
{
if (a[m]>TopK[0])
{
TopK[0] = a[m];
}
}

for (int n = 0; n < K; ++n)  //一次输出K个最大数
{
cout << TopK[n] << " ";
}
cout << endl;
}


#include"BIgData.h"

void TestTopK()
{
int a[N];
int TopK[K];

for (int i = 0; i < N; ++i)
{
a[i] = i;
}

GetTopK(a, TopK);

}
int main()
{
TestTopK();
system("pause");
return 0;
}


//2.堆排序：建大堆，每次找到最大的数据交换到数组末尾，将剩下的数据AdjustDown，再进行交换
void AdjustDown2(int a[],int size,size_t parent)
{
int child = parent * 2 + 1;

while (child<size)
{
if ((child + 1 < size)&&a[child] < a[child + 1])
{
++child;
}

if (a[child] > a[parent])
{
swap(a[child], a[parent]);
parent = child;
child = parent * 2 + 1;
}
else
{
break;
}
}
}

void Heap_Sort(int a[], size_t n)
{
for (int i = (n - 2) / 2; i >= 0; i--)  //注意边界条件
{
}

for (int i = 0; i < n; ++i)
{
swap(a[0], a[n - 1-i]);

AdjustDown2(a, n - 1 - i, 0);
}

for (int i = 0; i < n; ++i)
{
cout << a[i] << " ";
}
cout << endl;

}


void TestHeap_Sort()
{
int a[] = { 10, 12, 9, 15, 13, 17, 16, 18, 20,14 };
Heap_Sort(a, 10);
}

int main()
{
TestHeap_Sort();
system("pause");
return 0;
}


#### 优先队列——斐波那契堆(without source code)

2016-01-24 10:24:39

#### 数据结构::堆及堆的应用~

2016-12-31 13:47:35

#### [大、小根堆应用总结一]堆排序的应用场景

2016-05-10 09:53:55

#### 堆和堆的应用：堆排序和优先队列

2017-11-19 11:57:02

#### 数据结构(Java)——堆的应用

2015-12-01 20:14:42

#### 算法导论之二项堆

2016-08-01 14:52:23

#### 堆原理及其基本应用

2016-12-02 21:38:22

#### 算法导论之斐波那契堆

2016-08-05 11:28:12

#### 堆的一些简单应用

2016-06-01 19:01:46

#### 堆的简单应用——TopK

2018-05-14 00:47:41