堆排序就是選擇排序的演化版本。核心思想就是將數(shù)據(jù)源先整合成一個(gè)完全二叉樹,這樣在第一級(jí)節(jié)點(diǎn)的數(shù)據(jù)肯定是最大的洁闰,這樣數(shù)據(jù)比較就簡單很多了。其中堆排序有個(gè)牛逼閃閃的題万细,叫尋找最小的k個(gè)數(shù) 就這個(gè)鏈接里老哥說了很多解法扑眉,我大概就看了個(gè)對(duì)排序的解法。題目是假設(shè)數(shù)組[1,4,5,23,414,523,342,....]是個(gè)無序數(shù)組赖钞,那么腰素,想要找到K個(gè)最小數(shù),并且還要給K個(gè)最小數(shù)排序雪营。
解題核心思想就是先假定這個(gè)數(shù)組前K個(gè)數(shù)就已經(jīng)是我們要的結(jié)果弓千,然后把數(shù)組前K個(gè)數(shù)先拿出來,做個(gè)完全二叉樹的堆卓缰,這樣就是排好了一個(gè)結(jié)果集计呈,并且第一個(gè)元素肯定是最大的,因?yàn)橥耆鏄涞念^結(jié)點(diǎn)就是最大值(這種叫最大堆)征唬。然后再把這個(gè)頭結(jié)點(diǎn)和數(shù)據(jù)源剩下的數(shù)進(jìn)行比較捌显。所以,最難得點(diǎn)其實(shí)是將前K個(gè)數(shù)找出來并且做好一顆完全二叉樹(起碼這是我覺得難點(diǎn))总寒。
#include <iostream>
using namespace std;
/******************
根據(jù)輸入的int數(shù)組創(chuàng)建一個(gè)最大堆
input: 輸入的int數(shù)組
maxHeap: 最大堆數(shù)組
maxHeapCount 最大堆中元素的個(gè)數(shù)
******************/
void createMaxHeap(const int * input, int * maxHeap, const int maxHeapCount);
/******************
對(duì)輸入int數(shù)組進(jìn)行操作扶歪,使之符合最大堆的條件:
所有結(jié)點(diǎn)的值不大于其父結(jié)點(diǎn)
maxHeap: 最大堆數(shù)組
pos: 最大堆中的元素標(biāo)志位,由1開始
maxHeapCount 最大堆中元素的個(gè)數(shù)
******************/
void heapifyMaxHeap(int * maxHeap, const int pos, const int maxHeapCount);
/******************
對(duì)最大堆排序摄闸,使之由小到大有序
maxHeap: 最大堆數(shù)組
maxHeapCount 最大堆中元素的個(gè)數(shù)
******************/
void maxHeapSort(int * maxHeap, const int maxHeapCount);
/******************
根據(jù)指定的長度初始化輸入的int數(shù)組
bigData: 輸入的int數(shù)組
arrayLength: 數(shù)組長度
******************/
void initBigDataArray(int * bigData, const int arrayLength);
void main()
{
const int M = 1000;
const int K = 5;
int bigDataM[M];
int maxHeap[K];
initBigDataArray(bigDataM, M);
createMaxHeap(bigDataM, maxHeap, K);
for(int step = K; step < M; ++step)
{
if(bigDataM[step] < maxHeap[0])
{
maxHeap[0] = bigDataM[step];
heapifyMaxHeap(maxHeap, 1, K);
}
}
maxHeapSort(maxHeap, K);
cout<<"bigData array is: ";
for(int step = 0; step < M; ++step)
{
cout<<bigDataM[step]<<" ";
}
cout<<endl;
cout<<"Output maxHeap from less to larger: ";
for(int step = 0; step < K; ++step)
{
cout<<maxHeap[step]<<" ";
}
cout<<endl;
cout<<"Output maxHeap from larger to less: ";
for(int step = 0; step < K; ++step)
{
cout<<maxHeap[K - 1 - step]<<" ";
}
cout<<endl;
return;
}
void heapifyMaxHeap(int * maxHeap, const int pos, const int maxHeapCount)
{
int left = 2 * pos;
int right = 2 * pos + 1;
int largestElemPos = 0;
if(left <= maxHeapCount && maxHeap[left - 1] > maxHeap[pos - 1])
largestElemPos = left;
else
largestElemPos = pos;
if(right <= maxHeapCount && maxHeap[right - 1] > maxHeap[largestElemPos - 1])
largestElemPos = right;
if(largestElemPos != pos)
{
swap(maxHeap[pos - 1], maxHeap[largestElemPos - 1]);
heapifyMaxHeap(maxHeap, largestElemPos, maxHeapCount);
}
}
void createMaxHeap(const int * input, int * maxHeap, const int maxHeapCount)
{
for(int step = 0; step < maxHeapCount; ++step)
{
maxHeap[step] = input[step];
}
for(int startHeapifyPos = maxHeapCount/2; startHeapifyPos >= 1; --startHeapifyPos)
{
heapifyMaxHeap(maxHeap, startHeapifyPos, maxHeapCount);
}
}
void initBigDataArray(int * bigData, const int arrayLength)
{
for(int i = 0; i< arrayLength; ++i)
{
bigData[i] = rand();
}
}
void maxHeapSort(int * maxHeap, const int maxHeapCount)
{
int maxHeapSize = maxHeapCount;
for(int step = maxHeapSize; step >=2; --step)
{
swap(maxHeap[step - 1], maxHeap[0]);
maxHeapSize -=1;
heapifyMaxHeap(maxHeap, 1 , maxHeapSize);
}
}
==============java版=================
public class Solution {
public ArrayList<Integer> GetLeastNumbers_Solution(int[] input, int k) {
ArrayList<Integer> result = new ArrayList<Integer>();
int length = input.length;
if (k > length || k == 0) {
return result;
}
PriorityQueue<Integer> maxHeap = new PriorityQueue<Integer>(k, new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o2.compareTo(o1);
}
});
for (int i = 0; i < length; i++) {
if (maxHeap.size() != k) {
maxHeap.offer(input[i]);
} else if (maxHeap.peek() > input[i]) {
Integer temp = maxHeap.poll();
temp = null;
maxHeap.offer(input[i]);
}
}
for (Integer integer : maxHeap) {
result.add(integer);
}
return result;
}
}
其中JAVA提供了PriorityQueue這個(gè)類就是符合堆特性的隊(duì)列善镰,其內(nèi)部是數(shù)組結(jié)構(gòu),所以在構(gòu)建K個(gè)最小堆時(shí)候其實(shí)直接new一個(gè)并且循環(huán)放進(jìn)去就好了年枕。