算法导论c++ 堆排序以堆类结构完整实现

程序不当之处,欢迎指正

// HeapSort.cpp: 定义控制台应用程序的入口点。
#include "stdafx.h"
#include <vector>
#include <algorithm>  //swap;
#include <iostream>
using namespace std;

class Heap
{
    private:

        int size;
        void Max_heapify(vector<int> &A, int i,int s) {
            int l = Left(i);
            int r = Right(i);
            //找到比父节点大的子节点;
            int largest=i;
            if (l < s&&A[l] > A[i]) largest = l;
            if (r < s&&A[r] > A[largest]) largest = r;
            if (largest!= i)
            {
                swap(A[i],A[largest]);
                Max_heapify(A, largest,s);//防止子节点违反最大堆的性质;
            }


        }//维护堆的性质;

        void Build_Heap(){
            for (int i = size / 2 - 1; i >= 0; i--)
            {
                Max_heapify(heap, i,size);
            }
        }//构建堆;

    public:
        vector<int> heap;
        Heap(vector<int> array)
        {           
            heap.assign(array.begin(), array.end());
            size = array.size();
            Build_Heap();
        }
        int Parent(int index)//父节点;
        {
            if (index == 0) return 0;
            else return (index - 1) / 2;
        }
        int Left(int index)//左节点;
        {
            return 2 * index + 1;
        }
        int Right(int index)//右节点;
        {
            return Left(index) + 1;
        }
        int HeapSize()
        {
            return size;
        }

        //优先队列;
        int MaxIMum()//返回最大值;
        {
            return heap[0];
        }
        int Extract_Max()//去除最大值并返回最大值;
        {
            if (size< 1) printf("heap underflow");
            int max = heap[0];
            heap[0] = heap[size-1];

            size--;
            heap.pop_back();
            Max_heapify(heap,0,size);
            return max;

        }
        void InsertKey(int x,int key)//x索引值增加到key(key<x处值);
        {
            if (key < heap[x]) printf("new key is smaller than current key");
            else
            {
                heap[x] = key;
                while (x > 0 && heap[Parent(x)] < heap[x])
                {
                    swap(heap[x],heap[Parent(x)]);
                    x = Parent(x);
                }
            }

        }
        void Insert(int k)//插入值;
        {
            size++;
            heap.push_back(INT_MIN);
            InsertKey(size - 1, k);
        }

        vector<int> Heap_Sort()//堆排序;
        {
            vector<int> A;//存储排序的数组;
            A.assign(heap.begin(), heap.end());;
            int s = size;
            for (int i = s-1; i >0; i--)
            {
                swap(A[0], A[i]);
                s--;

                Max_heapify(A,0,s);
            }
            return A;//返回从小到大排序好的数组;
        }

};

int main()
{
    vector<int> Test{ 16,14,10,8,7,9,3,2,4,1 };
    Heap newHeap =Heap(Test);
    //测试节点的获取;
    cout << "Parent " << newHeap.Parent(3)<<endl;//2
    cout << "Left " << newHeap.Left(3) << endl;//7
    cout << "Right " << newHeap.Right(3) << endl;//8

    //测试堆性质成功维护;
    for each (int a in newHeap.heap)
    {
        cout << a << " " ;
    }
    cout << endl;

    //测试排序结果;
    vector<int> B = newHeap.Heap_Sort();
    for each (int a in B)
    {
        cout << a << " ";
    }
    cout << endl;

    //测试sort不影响原排序;
    for each (int a in newHeap.heap)
    {
        cout << a << " ";
    }
    cout << endl;

    //测试最大值;
    cout << "Max " << newHeap.MaxIMum() << endl;

    //测试Extract_Max();
    cout << newHeap.Extract_Max() << endl;
    for each (int a in newHeap.heap)
    {
        cout << a << " ";
    }
    cout << endl;

    //测试InsertKey;
    newHeap.InsertKey(5, 100);
    for each (int a in newHeap.heap)
    {
        cout << a << " ";
    }
    cout << endl;

    newHeap.Insert(34);
    for each (int a in newHeap.heap)
    {
        cout << a << " ";
    }
    cout << endl;
    while(true){}
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值