HeapSort C++

Heaps

 

Heap Definition

  A max tree(min tree) is a tree in which the value in each node is greater(less) than or equal to those in its children(if any)    

Building a max heap

  Look at below figure, we adjust elements in a array, swap some elements, at last we have a max heap. The progress begin from the last smallest heap. If it is a illegal max heap, we swap elements to let it became a legal max heap. Look at (b) in the figure. Three elements 2, 14, 8 is not a max heap. We adjust the tree by swaping 2 and 14, then it is a max heap, looking at (c) in the figure.      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
int a[10]={4,1,3,2,16,9,10,14,8,7};
for(int i = 4; i >= 0; i--){
                   int leftIndex = i * 2 + 1;     // i is the node index, so the node's left child's index is i * 2 + 1
                                          // notice: in an array , 0 is the first index.
 
           while(leftIndex < n){          // if we do not have the left child, quit the loop
             int left = a[leftIndex];    
 
       //---------------find the max element in the three element, and exchange the two elements.  begin----------------------
 
             int maxIndex = i;            // In a 3 elements heap, we assume the node is the max element.
             if(left > a[i]){             // If node's left child value is big than node
               maxIndex = leftIndex;      // we change the maxIndex value to left child's index
             }
             if(leftIndex + 1 < n && a[leftIndex + 1] > a[maxIndex]){ //if we have the right child, and right child's value is big
                                                                      // than the max value of the two(node and its left child)
               maxIndex = leftIndex + 1;                              // we change the maxIndex value to right child's index
             }
             if(maxIndex != i){           // if some child of the node is big than the node's value
               swap(a[i], a[maxIndex]);   // we exchange the two elements     
             }else{
                 break;
             }
 
      //---------------find the max element in the three element, and exchange the two elements.  end ----------------------
 
             i = maxIndex;                // Because we exchange some value,so we will look down from the element we had changed,
                                          // So we change the node index to the max value's index,change the left index
             leftIndex = 2 * i + 1;      
           }
    }

HeapSort From a max heap

    First, we swap the first element and last element, and rebuild the max heap of the elments except the last element. To do such thing in the new max heap until there is only one element.    
1
2
3
4
5
6
7
int a[10]={4,1,3,2,16,9,10,14,8,7};
BuildMaxHeap(a, 10);// build max heap
// heap sort
for(int i = 9; i >= 1;i--){
        swap(a[0],a[i]);
        MaxHeapify(a, 0, i); // rebuild max heap
    }
  Whole Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// HeapSort.cpp : Defines the entry point for the console application.
//
 
#include "stdafx.h"
#include <iostream>
 
using namespace std;
 
//A easy version, but it is not the best one.
 
/*
template <class T>
void MaxHeapify(T a[], int i, int n){
           int leftIndex = i * 2 + 1;     // i is the node index, so the node's left child's index is i * 2 + 1
                                          // notice: in an array , 0 is the first index.
 
           while(leftIndex < n){          // if we do not have the left child, quit the loop
             int left = a[leftIndex];    
 
       //---------------find the max element in the three element, and exchange the two elements.  begin----------------------
 
             int maxIndex = i;            // In a 3 elements heap, we assume the node is the max element.
             if(left > a[i]){             // If node's left child value is big than node
               maxIndex = leftIndex;      // we change the maxIndex value to left child's index
             }
             if(leftIndex + 1 < n && a[leftIndex + 1] > a[maxIndex]){ //if we have the right child, and right child's value is big
                                                                      // than the max value of the two(node and its left child)
               maxIndex = leftIndex + 1;                              // we change the maxIndex value to right child's index
             }
             if(maxIndex != i){           // if some child of the node is big than the node's value
               swap(a[i], a[maxIndex]);   // we exchange the two elements     
             }else{
                 break;
             }
 
      //---------------find the max element in the three element, and exchange the two elements.  end ----------------------
 
             i = maxIndex;                // Because we exchange some value,so we will look down from the element we had changed,
                                          // So we change the node index to the max value's index,change the left index
             leftIndex = 2 * i + 1;      
           }
}*/
 
 //The best one.
template <class T>
//adjust elements from i to n.
void MaxHeapify(T a[], int i, int n) 
    int leftIndex, node; 
 
    node = a[i]; 
    leftIndex = 2 * i + 1; 
    while (leftIndex < n) 
    
 
        //---------- finding a max element in left child and right child--------------
        if (leftIndex + 1 < n && a[leftIndex + 1] > a[leftIndex])  
            leftIndex++; 
        //---------- finding a max element in left child and right child--------------
        if (a[leftIndex] <= node) 
            break
 
        a[i] = a[leftIndex];     
        i = leftIndex; 
        leftIndex = 2 * i + 1; 
    
    a[i] = node; 
}
 
template <class T>
void BuildMaxHeap(T a[], int n){
    for(int i = n/2 - 1; i >= 0; i--){
        MaxHeapify(a, i ,n);
    }
}
 
template <class T>
void HeapSort(T a[], int n){
    for(int i = n - 1; i >= 1;i--){
        swap(a[0],a[i]);
        MaxHeapify(a, 0, i); // rebuild max heap
    }
}
 
template <class T>
void PrintfNum(T a[], int n);
 
int main(int argc, char* argv[])
{
    const int NUM = 10;
    int a[NUM]={4,1,3,2,16,9,10,14,8,7};
    cout << "Before sort:" << endl;
    int flag;
    PrintfNum(a, NUM);
 
    cout << "Build Max Heap:" << endl;
    BuildMaxHeap(a, NUM);// building a max heap
    PrintfNum(a, NUM);
 
    cout << "After Heap Sort:" << endl;
    HeapSort(a, NUM);
    PrintfNum(a, NUM);
 
    return 0;
}
 
template <class T>
void PrintfNum(T a[], int n){
    for(int i = 0; i < n; i++){
        cout << a[i] << ",";
    }
    cout << endl;
}
http://www.easycpp.com/?p=15
posted @   easycpp  阅读(749)  评论(0)    收藏  举报
编辑推荐:
· 记一次 .NET某固高运动卡测试 卡慢分析
· 微服务架构学习与思考:微服务拆分的原则
· 记一次 .NET某云HIS系统 CPU爆高分析
· 如果单表数据量大,只能考虑分库分表吗?
· 一文彻底搞懂 MCP:AI 大模型的标准化工具箱
阅读排行:
· 7 个最近很火的开源项目「GitHub 热点速览」
· DeepSeekV3:写代码很强了
· 博客园2025新款「AI繁忙」系列T恤上架
· 记一次 .NET某固高运动卡测试 卡慢分析
· Visual Studio 2022 v17.13新版发布:强化稳定性和安全,助力 .NET 开发提
点击右上角即可分享
微信分享提示