用类实现堆排序

heap.h:

#ifndef HEAP_H
#define	HEAP_H
#define N 100
class Heap{
public:
    Heap();
    void max_heapify(int);
    void build_max_heap();
    void max_heap_sort();
    void max_heap_insert(int);
    int extract_max();
    int heap_max(){
        return num[1];
    }
    int for_sort[N];
    int num[N];
    int heap_size;
};


#endif

heap.cpp:

#include "heap.h"
#include <iostream>
using namespace std;

Heap::Heap(){
    for(int i = 0; i < N; i++){
        num[i] = 0;
        for_sort[i] = 0;
    }
    heap_size = 0;
}

void Heap::max_heapify(int i){
    int l = 2*i;
    int large = i;       //care the initialization
    if(l <= heap_size)
        large = (num[i] > num[l]) ? i : l;
    if(l+1 <= heap_size)
        large = (num[large] > num[l+1]) ? large : l+1;
    if(large != i){
        int tmp = num[i];
        num[i] = num[large];
        num[large] = tmp;
        max_heapify(large);
    }
}

void Heap::build_max_heap(){
    for(int i = heap_size/2; i > 0; i--)
        max_heapify(i);
}

void Heap::max_heap_sort(){
    int tmp = heap_size;
    for(int i = heap_size; i > 1; i--){
        int tmp = num[i];
        num[i] = num[1];
        num[1] = tmp;
        heap_size--;
        max_heapify(1);
    }
    heap_size = tmp;
    for(int i = 1; i <= heap_size; i++)
        for_sort[i] = num[i];
    build_max_heap();
}

void Heap::max_heap_insert(int number){
    if(heap_size == N-1)
        cout << "failure:heap is full!\n";
    else{
        num[++heap_size] = number;
        int i = heap_size;
        while(i > 1){
            if(num[i] <= num[i/2])
                break;
            int tmp = num[i];
            num[i] = num[i/2];
            num[i/2] = tmp;
            i /= 2;
        }
    }
}

int Heap::extract_max(){
    if(heap_size == 0)
        cout << "failure:heap is empty!\n";
    else{
        int tmp = num[heap_size];
        num[heap_size] = num[1];
        num[1] = tmp;
        heap_size--;
        max_heapify(1);
        return num[heap_size+1];
    }
    return 0;
}

main.cpp(用于测试):

#include "heap.h"
#include <iostream>
using namespace std;

int main() {
    Heap a;
    int b;
    for(int i = 1; i < 11; i++){
        cin >> a.num[i];
        a.heap_size++;
    }
    a.build_max_heap();
    cout << a.heap_max() << endl;
    for(int i = 0; i < 5; i++){
        cin >> b;
        a.max_heap_insert(b);
    }
    
    cout << "the max:" << a.extract_max() << endl;
    a.max_heap_sort();
    for(int i = 1; i < a.heap_size+1; i++)
        cout << a.for_sort[i] << "  ";
    cout << "\n" << a.heap_size << endl;

    for(int i = 0; i < 5; i++){
        cin >> b;
        a.max_heap_insert(b);
    }
    a.max_heap_sort();
    for(int i = 1; i < a.heap_size+1; i++)
        cout << a.for_sort[i] << "  ";
    cout << "\n" << a.heap_size << endl;

    return 0;
}


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

刀么克瑟拉莫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值