Heap Sort in C++
2006-04-29 17:06
369 查看
/*
This function is an important subroutine for manipulating max-heaps. Its inputs are an array A with it's length and an index i into the array.
When maxHeapify is called, it is assumed that the binary trees rooted at it's children are max-heaps, but that A[i] may be smaller than its children, thus violating the max-heap property. The function is to let the value at A[i] "float down" in the max-heap so that the subtree rooted at index i becomes a max-heap.
*/
void maxHeapify(int A[], int length, int i) {
int left = 2 * i;
int right = 2 * i + 1;
int largest = i;
if (left < length && A[left] > A[largest]) {
largest = left;
}
if (right < length && A[right] > A[largest]) {
largest = right;
}
if (largest != i) {
int temp = A[i];
A[i] = A[largest];
A[largest] = temp;
maxHeapify(A, length, largest);
}
}
/*
The recursive version of maxHeapify above might cause some compilers to produce inefficient code. Below is an iterative version.
*/
void maxHeapifyInLoop(int A[], int length, int i) {
int cur = i;
int left;
int right;
int largest;
int temp;
while (cur < length) {
left = 2 * cur;
right = 2 * cur + 1;
largest = cur;
if (left < length && A[left] > A[largest]) {
largest = left;
}
if (right < length && A[right] > A[largest]) {
largest = right;
}
if (largest == cur) break;
// switch A[cur] and A[largest]
temp = A[cur];
A[cur] = A[largest];
A[largest] = temp;
// change current node to largest
cur = largest;
}
}
/*
We can use the procedure "maxHeapify" in a bottom-up manner to convert an array A[0...(n-1)], where n = length[A], into a max-heap. The elements in the subarray A[((n-1)/2+1)...(n-1)] are all leaves of the tree, and so each is a 1-element heap to begin with. The procedure "buildMaxHeap" goes through the remaining nodes of the tree and runs "maxHeapify" on each one.
*/
void buildMaxHeap(int A[], int length) {
for (int i = (length-1)/2; i >= 0; i--) {
maxHeapify(A, length, i);
}
}
/*
The heapsort algorithm starts by using "buildMaxHeap" to build a max-heap on the input array A[0...(n-1)], where n = length[A]. Since the maximum element of the array is stored at the root A[0], it can be put into its correct final position by exchanging it with A[n-1]. If we now "discard" node (n-1) from the heap (by decrementing heap-size[A]), we observe that A[0...(n-2)] can easily be made into a max-heap. The children of the root remain max-heaps, but the new root element may violate the max-heap property. All that is needed to restore the maxheap property, however, is one call to maxHeapify(A, (n-1), 0), which leaves a max-heap in A[0...(n-2)]. The heapsort algorithm then repeats this process for the max-heap of size (n-1)
down to a heap of size 2.
*/
void heapSort(int A[], int length) {
buildMaxHeap(A, length);
for (int i = length - 1; i > 0; i--) {
int temp = A[i];
A[i] = A[0];
A[0] = temp;
maxHeapify(A, i, 0);
}
}
This function is an important subroutine for manipulating max-heaps. Its inputs are an array A with it's length and an index i into the array.
When maxHeapify is called, it is assumed that the binary trees rooted at it's children are max-heaps, but that A[i] may be smaller than its children, thus violating the max-heap property. The function is to let the value at A[i] "float down" in the max-heap so that the subtree rooted at index i becomes a max-heap.
*/
void maxHeapify(int A[], int length, int i) {
int left = 2 * i;
int right = 2 * i + 1;
int largest = i;
if (left < length && A[left] > A[largest]) {
largest = left;
}
if (right < length && A[right] > A[largest]) {
largest = right;
}
if (largest != i) {
int temp = A[i];
A[i] = A[largest];
A[largest] = temp;
maxHeapify(A, length, largest);
}
}
/*
The recursive version of maxHeapify above might cause some compilers to produce inefficient code. Below is an iterative version.
*/
void maxHeapifyInLoop(int A[], int length, int i) {
int cur = i;
int left;
int right;
int largest;
int temp;
while (cur < length) {
left = 2 * cur;
right = 2 * cur + 1;
largest = cur;
if (left < length && A[left] > A[largest]) {
largest = left;
}
if (right < length && A[right] > A[largest]) {
largest = right;
}
if (largest == cur) break;
// switch A[cur] and A[largest]
temp = A[cur];
A[cur] = A[largest];
A[largest] = temp;
// change current node to largest
cur = largest;
}
}
/*
We can use the procedure "maxHeapify" in a bottom-up manner to convert an array A[0...(n-1)], where n = length[A], into a max-heap. The elements in the subarray A[((n-1)/2+1)...(n-1)] are all leaves of the tree, and so each is a 1-element heap to begin with. The procedure "buildMaxHeap" goes through the remaining nodes of the tree and runs "maxHeapify" on each one.
*/
void buildMaxHeap(int A[], int length) {
for (int i = (length-1)/2; i >= 0; i--) {
maxHeapify(A, length, i);
}
}
/*
The heapsort algorithm starts by using "buildMaxHeap" to build a max-heap on the input array A[0...(n-1)], where n = length[A]. Since the maximum element of the array is stored at the root A[0], it can be put into its correct final position by exchanging it with A[n-1]. If we now "discard" node (n-1) from the heap (by decrementing heap-size[A]), we observe that A[0...(n-2)] can easily be made into a max-heap. The children of the root remain max-heaps, but the new root element may violate the max-heap property. All that is needed to restore the maxheap property, however, is one call to maxHeapify(A, (n-1), 0), which leaves a max-heap in A[0...(n-2)]. The heapsort algorithm then repeats this process for the max-heap of size (n-1)
down to a heap of size 2.
*/
void heapSort(int A[], int length) {
buildMaxHeap(A, length);
for (int i = length - 1; i > 0; i--) {
int temp = A[i];
A[i] = A[0];
A[0] = temp;
maxHeapify(A, i, 0);
}
}
相关文章推荐
- Sort and Search in C and C++
- Leetcode OJ : Merge k Sorted Lists 归并排序+最小堆 mergesort heap C++ solution
- algorithm: heap sort in python 算法导论 堆排序
- HeapSort C++
- C++ make_heap,push_heap,pop_heap,sort_heap(以最大的K个数为例)
- 数据结构 - 堆排序(heap sort) 具体解释 及 代码(C++)
- C++ 堆排序 (HeapSort)
- Stack & Heap in C/C++ .
- OD: Memory Attach Technology - Off by One, Virtual Function in C++ & Heap Spray
- The Usage of Lambda and Heap in the C++ STL
- PAT程序设计考题——甲级1098(Insertion or Heap Sort ) C++实现
- 堆排序 heapsort(大根堆) c++
- c++: c++ make_heap, pop_heap, push_heap, sort_heap详解
- C++中堆(优先队列)的应用:make_heap, pop_heap, push_heap, sort_heap, priority_queue
- C++、C#、java算法学习日记07----堆排序(HeapSort)
- Merge sort in C++
- Stack & Heap in C/C++
- heap_sort-----C++
- 堆 build_heapify() heap_sort() C++实现
- C++中堆的应用:make_heap, pop_heap, push_heap, sort_heap, priority_queue