前言
堆一般是由數組實現的完全二叉樹,堆的排序也屬于選擇排序,JAVA jdk中的PriorityQueue就是采用的小根堆實現的升序排序,因此要了解PriorityQueue就必須掌握堆的排序,這里就采用大根堆方式來實現默認降序方式的PriorityQueue
一:堆排序
堆排序步驟:
1: 將無序數組構造成一個大根堆(或小根堆), 大(小)根堆的構造就是將最后一個非葉子結點到根節點進行大小調整(結點的調整從上至下)以滿足大(小)根堆的概念
直接上圖(圖中采用的是大根堆方式),排序無序數組 12 2 6 30 17 5 22 18
大根堆調整.png
從圖中可以看出最后一個非葉子結點是30, 就需要依次調整 30 6 2 12結點
2:將堆根(數組首元素)與末尾元素交換, 即相當于使數組末尾元素為最大, 再將除已經排列出的最大的尾元素前的數組繼續大(小)根堆調整, 由于第一步已經排列出大(小)根堆, 此時只需要直接從根節點開始向下調整即可
替換并調整.png
3:代碼實現(大根堆實現升序)
class HeapSort {
public:
/**
* 堆排序
* @param arrs
* @param len
*/
static void sort(int *arrs, int len) {
for (int i = (len >> 1) - 1; i >= 0; --i) {
siftDown(arrs, len, i);
}
for (int i = len - 1; i > 0 ; --i) {
swap(arrs, 0, i);
siftDown(arrs, i, 0);
}
}
/**
* 從上至下調整大根堆
* @param arrs
* @param len
* @param index
*/
static void siftDown(int *arrs, int len, int index) {
while (index < len >> 1) {
int maxChildIndex = (index << 1) + 1; //左右孩子中找出最大值的孩子的位置
int rightChildIndex = maxChildIndex + 1;
if (rightChildIndex < len && arrs[rightChildIndex] > arrs[maxChildIndex]) {
maxChildIndex = rightChildIndex;
}
if (arrs[maxChildIndex] <= arrs[index]) return;
swap(arrs, index, maxChildIndex);
index = maxChildIndex;
}
}
/**
* 交換
* @param arrs
* @param l
* @param r
*/
static void swap(int *arrs, int l, int r) {
int tmp = arrs[l];
arrs[l] = arrs[r];
arrs[r] = tmp;
}
};
二:PriorityQueue降序實現
PriorityQueue原理與堆排序類似,由于每次在隊列中加入元素的時候前面的元素已經做好了大根堆調整,所以每次在隊列中加入元素的時候,從下至上與父結點比較,小于父節點不做處理, 大于父節點時與父節點替換,再與父點節比較,刪除節點時與堆排序的第2步一致
直接上代碼
template <class E>
struct Greater {
constexpr bool operator() (const E &left, const E &right) const {
return left > right;
}
};
template <class E>
struct LessEqual {
constexpr bool operator() (const E &left, const E &right) const {
return left <= right;
}
};
template <class E, class C = Greater<E> >
class PriorityQueue {
private:
E *queue;
//類似JDK中的Comparable
C comparator;
int len = 0;
//初始數組大小
int capacity = 11;
/**
* 擴充數組
* @param minCapacity
*/
void grow();
/**
* 從下往上調整根堆
* @param index
* @param v
*/
void siftUp(int index, const E &e);
/**
* 從上往下調整根堆
* @param index
* @param v
*/
void siftDown(int index, const E &e);
public:
PriorityQueue();
PriorityQueue(int capacity);
~PriorityQueue();
//隊列是否空
bool isEmpty();
/**
* 優先隊列中添加元素
* @param e
*/
void push(const E &e);
/**
* 彈出隊首元素
* @return
*/
E poll();
//不彈出,查看首元素
E peek();
};
//默認大小11
template <class E, class C>
PriorityQueue<E, C>::PriorityQueue() : PriorityQueue(11) {
}
template <class E, class C>
PriorityQueue<E, C>::PriorityQueue(int capacity) {
assert(capacity > 1);
this->capacity = capacity;
this->queue = (E*) malloc(sizeof(E) * capacity);
}
template <class E, class C>
PriorityQueue<E, C>::~PriorityQueue() {
if (this->queue) {
free(this->queue);
this->queue = NULL;
}
}
/**
* 這里忽略擴充數組后大小超過int最大值
* @tparam E
* @tparam C
* @param minCapacity
*/
template <class E, class C>
void PriorityQueue<E, C>::grow() {
//擴充前的數組長度超過64時擴充1.5倍
capacity = capacity + ((capacity < 64) ? (capacity + 2) : (capacity >> 1));
queue = (E*) realloc(queue, sizeof(E) * capacity);
}
template <class E, class C>
void PriorityQueue<E, C>::siftUp(int index, const E &e) {
int parentIndex;
while (index > 0) {
parentIndex = (index - 1) >> 1;//找出父節點的位置
if (comparator(queue[parentIndex], e)) {//父節點大于該節點,跳出循環
break;
}
queue[index] = queue[parentIndex];
index = parentIndex;
}
queue[index] = e;
}
template <class E, class C>
void PriorityQueue<E, C>::siftDown(int index, const E &e) {
while (index < len >> 1) {
int maxChildIndex = (index << 1) + 1;//左孩子與右孩子比較得出最大孩子的位置
int rightChildIndex = maxChildIndex + 1;
if (rightChildIndex < len && comparator(queue[rightChildIndex], queue[maxChildIndex])) {
maxChildIndex = rightChildIndex;
}
if (!comparator(queue[maxChildIndex], e)) {
break;
}
queue[index] = queue[maxChildIndex];
index = maxChildIndex;
}
queue[index] = e;
}
template <class E, class C>
bool PriorityQueue<E, C>::isEmpty() {
return len <= 0;
}
template <class E, class C>
void PriorityQueue<E, C>::push(const E &e) {
if (this->len >= capacity) {
grow();
}
siftUp(this->len, e);
this->len++;
}
template <class E, class C>
E PriorityQueue<E, C>::poll() {
assert(len > 0);
E max = queue[0];
this->len--;
if (this->len > 0) {
siftDown(0, queue[len]);
}
return max;
}
template <class E, class C>
E PriorityQueue<E, C>::peek() {
assert(len > 0);
return queue[0];
}